Parse Query Cookbook in React Native
Introduction
We’ve already seen how a Parse.Query
with get
can retrieve a single Parse.Object
from Back4App. There are many other ways to retrieve data with Parse.Query
- you can retrieve many objects at once, use conditions on the objects you wish to retrieve, and more.
In this guide, you will ding deep into the Parse.Query
class and see all the methods you can use to build your Queries. You will use a simple database class with some mocked data to perform the Queries using the Javascript Console on Back4App.
Prerequisites
To complete this tutorial, you will need:
- An App created on Back4App.
Goal
Explore the Parse.Query
class different methods.
The Parse.Query class
Any query operation on Parse uses the Parse.Query
object type, which will help you retrieve specific data from your Back4App throughout your app. To create a new Parse.Query
, you need to pass as a parameter the desired Parse.Object
subclass, which is the one that will contain your query results.
It is crucial to know that a Parse.Query
will only resolve after calling a retrieve method (like Parse.Query.find
or Parse.Query.get
), so a query can be set up and several modifiers can be chained before actually being called.
You can read more about the Parse.Query
class here at the official documentation.
Using the JS Console on Back4App
Inside your Back4App application’s dashboard, you will find a very useful API console in which you can run JavaScript code directly. In this guide you will use to store and query data objects from Back4App. On your App main dashboard go to Core->API Console->JS Console
.
Save your Data Objects
To run the queries on this guide you’ll need first to populate your App with some data. Let’s create a sample class called Profile
, which mocks a social media profile class using famous people names and the following fields:
- string type
name
; - Date type
birthDay
; - Number (integer) type
friendCount
; - Array (string array) type
favoriteFoods
; - Array (Number array) type
luckyNumbers
; - GeoPoint type
lastLoginLocation
; - Nullable pointer type
premiumMembership
, related to aMembership
class containing stringname
and DateexpirationDate
fields.
Here is the Parse.Object
classes creation code, so go ahead and run it in your API console:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Add Profile objects and create table
// Adam Sandler
let Profile = new Parse.Object('Profile');
Profile.set('name', 'Adam Sandler');
Profile.set('birthDay', new Date('09/09/1966'));
Profile.set('friendCount', 2);
Profile.set('favoriteFoods', ['Lobster', 'Bread']);
Profile.set('luckyNumbers', [2, 7]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
await Profile.save();
// Britney Spears
Profile = new Parse.Object('Profile');
Profile.set('name', 'Britney Spears');
Profile.set('birthDay', new Date('12/02/1981'));
Profile.set('friendCount', 52);
Profile.set('favoriteFoods', ['Cake', 'Bread']);
Profile.set('luckyNumbers', [22, 7]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
await Profile.save();
// Carson Kressley
Profile = new Parse.Object('Profile');
Profile.set('name', 'Carson Kressley');
Profile.set('birthDay', new Date('11/11/1969'));
Profile.set('friendCount', 12);
Profile.set('favoriteFoods', ['Fish', 'Cookies']);
Profile.set('luckyNumbers', [8, 2]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
await Profile.save();
// Dan Aykroyd
// Creates related object Membership for this user only
let Membership = new Parse.Object('Membership');
Membership.set('name', 'Premium');
Membership.set('expirationDate', new Date('10/10/2030'))
await Membership.save();
Profile = new Parse.Object('Profile');
Profile.set('name', 'Dan Aykroyd');
Profile.set('birthDay', new Date('07/01/1952'));
Profile.set('friendCount', 66);
Profile.set('favoriteFoods', ['Jam', 'Peanut Butter']);
Profile.set('luckyNumbers', [22, 77]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
Profile.set('premiumMembership', Membership);
await Profile.save();
// Eddie Murphy
Profile = new Parse.Object('Profile');
Profile.set('name', 'Eddie Murphy');
Profile.set('birthDay', new Date('04/03/1961'));
Profile.set('friendCount', 49);
Profile.set('favoriteFoods', ['Lettuce', 'Pepper']);
Profile.set('luckyNumbers', [6, 5]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(-27.104919974838154, -52.61428045237739));
await Profile.save();
// Fergie
Profile = new Parse.Object('Profile');
Profile.set('name', 'Fergie');
Profile.set('birthDay', new Date('03/27/1975'));
Profile.set('friendCount', 55);
Profile.set('favoriteFoods', ['Lobster', 'Shrimp']);
Profile.set('luckyNumbers', [13, 7]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(-27.104919974838154, -52.61428045237739));
await Profile.save();
console.log('Success!');
After running this code, you should now have a Profile
class in your database with six objects created. Your new class should look like this:
Let’s now take a look at examples from every Parse.Query
method, along with brief explanations on what they do. Please note that some methods in this list can take options
as an additional argument, but in most cases, it is only related to masterKey
usage and not relevant to this guide content, so this possibility will be omitted whenever not relevant.
Query retrievers
These methods are responsible for running the query and retrieving its results, being always present in your query implementation.
Cancels the current network request.
1
2
3
4
5
6
let query = new Parse.Query('Profile');
let queryResult = await query.find();
// This is hard to test in small databases, since by the time
// "cancel" is called, the "find" request is already resolved
queryResult = query.cancel();
console.log(queryResult);
Retrieves the count of Parse.Object
results that meet the query criteria.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.count();
console.log(queryResult);
Runs the query and returns a list of unique values from the results and the specified key.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.distinct('favoriteFoods');
console.log(queryResult);
This is the basic method for retrieving your query results, always returning an array of Parse.Object
instances, being empty when none are found.
1
2
3
4
5
let query = new Parse.Query('Profile');
// When using .find() in a query without other operations, you will
// get every object saved in your class
let queryResult = await query.find();
console.log(queryResult);
Retrieves a complete list of Parse.Objects
that satisfy this query.
1
2
3
4
5
let query = new Parse.Query('Profile');
// When using .findAll() in a query without other operations, you will
// get every object saved in your class
let queryResult = await query.findAll();
console.log(queryResult);
Retrieves the first Parse.Object
instance that meets the query criteria.
1
2
3
4
let query = new Parse.Query('Profile');
// Pay attention to your query ordering when using .first
let queryResult = await query.first();
console.log(queryResult);
This quick method is used to retrieve a single Parse.Object
instance when you know its objectId.
1
2
3
4
5
let query = new Parse.Query('Profile');
// Since objectId is randomly set in your database, make sure to inform a
// valid one when testing this method
let queryResult = await query.get('C6ENdLnFdQ');
console.log(queryResult);
Query conditioners
These methods give you the possibility of applying conditional constraints to your query, which are arguably the most important operations in querying. Remember that these operations can all be chained before the results are retrieved, so many combinations can be achieved to solve your querying needs.
Helper that is called by Parse for filtering objects using conditional constants, such as “$gt”, “$eq” and so on. Only usable by users in very specific cases.
1
2
3
4
let query = new Parse.Query('Profile');
query._addCondition('friendCount', '$gt', 25);
let queryResult = await query.find();
console.log(queryResult);
Helper used by Parse that converts string for regular expression at the beginning.
1
2
3
let query = new Parse.Query('Profile');
let result = query._regexStartWith('text');
console.log(result);
Filters objects in which a key value must be contained by the provided array of values. Get objects where all array elements match.
1
2
3
4
let query = new Parse.Query('Profile');
query.containedBy('luckyNumbers', [2, 7]);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a key value is contained in the provided array of values.
1
2
3
4
5
let query = new Parse.Query('Profile');
// containedIn can be used on any data type such as numbers and strings
query.containedIn('luckyNumbers', [2, 7]);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a string key value contains the provided text value. Be aware that this condition is case-sensitive.
1
2
3
4
5
let query = new Parse.Query('Profile');
// This can be slow in large databases and are case sensitive
query.contains('name', 'da');
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which an array type key value must contain every value provided.
1
2
3
4
let query = new Parse.Query('Profile');
query.containsAll('luckyNumbers', [2, 7]);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which an array type key value must contain every string value provided.
1
2
3
4
5
let query = new Parse.Query('Profile');
// These should be string values
query.containsAllStartingWith('favoriteFoods', ['Shrimp', 'Lobster']);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a key value is not set.
1
2
3
4
let query = new Parse.Query('Profile');
query.doesNotExist('premiumMembership');
let queryResult = await query.find();
console.log(queryResult);
Requires that a key’s value does not match a value in an object returned by a different Parse.Query. Useful for multi-object querying.
1
2
3
4
5
6
7
let query = new Parse.Query('Profile');
// Multiple queries can be combined using this, useful when there are more objects
// related, not our example case
query.doesNotMatchKeyInQuery('friendCount', 'friendCount', new Parse.Query('Profile').lessThan('friendCount', 50));
query.greaterThan('friendCount', 10);
let queryResult = await query.find();
console.log(queryResult);
Requires that an object contained in the given key does not match another query. Useful for multi-object querying.
1
2
3
4
5
6
7
let innerQuery = new Parse.Query('Membership');
innerQuery.greaterThan('expirationDate', new Date());
let query = new Parse.Query('Profile');
query.exists('premiumMembership');
query.doesNotMatchQuery('premiumMembership', innerQuery);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a string key’s value ends with the provided text value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// This is faster than other string searches by using backend index
query.endsWith('name', 'ie');
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a specific key’s value is equal to the provided value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// equalTo can be used in any data type
query.equalTo('friendCount', 2);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a key value is set.
1
2
3
4
let query = new Parse.Query('Profile');
query.exists('premiumMembership');
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a string key’s value wrap matches the provided text value in it. It can have many customizable options to improve its results, like language specification and score order. Check the Parse docs to have a complete understanding of that.
1
2
3
4
5
6
7
8
9
let query = new Parse.Query('Profile');
// fullText can be very powerful in text search queries, but
// also slow in large datasets when its options are not optimized
query.fullText('name', 'Spears', { diacriticSensitive: false, caseSensitive: false });
// In order to sort you must use select and ascending ($score is required)
query.ascending('$score');
query.select('$score');
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a specific key’s value is greater than the provided value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// greaterThan can be used on numbers and dates
query.greaterThan('birthDay', new Date('08/19/1980'));
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a specific key’s value is greater than or equal to the provided value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// greaterThanOrEqualTo can be used on numbers and dates
query.greaterThanOrEqualTo('friendCount', 49);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a specific key’s value is lesser than the provided value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// lessThan can be used on numbers and dates
query.lessThan('birthDay', new Date('08/19/1980'));
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a specific key’s value is lesser than or equal to the provided value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// lessThanOrEqualTo can be used on numbers and dates
query.lessThanOrEqualTo('friendCount', 49);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a string type key value must match the provided regular expression and its modifiers.
1
2
3
4
5
6
let query = new Parse.Query('Profile');
// Using the "i" modifier is a quick way to achieve
// case insensitive string querying
query.matches('name', 'da', 'i');
let queryResult = await query.find();
console.log(queryResult);
Requires that a key’s value matches a value in an object returned by a different Parse.Query. Useful for multi-object querying.
1
2
3
4
5
6
7
let query = new Parse.Query('Profile');
// Multiple queries can be combined using this, useful when there are more objects
// related, not our example case
query.matchesKeyInQuery('friendCount', 'friendCount', new Parse.Query('Profile').lessThan('friendCount', 50));
query.greaterThan('friendCount', 10);
let queryResult = await query.find();
console.log(queryResult);
Requires that an object contained in the given key matches another query. Useful for multi-object querying.
1
2
3
4
5
6
7
let innerQuery = new Parse.Query('Membership');
innerQuery.greaterThan('expirationDate', new Date());
let query = new Parse.Query('Profile');
query.exists('premiumMembership');
query.matchesQuery('premiumMembership', innerQuery);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a specific key’s value is not equal to the provided value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// notEqualTo can be used in any data type
query.notEqualTo("friendCount", 2);
let queryResult = await query.find();
console.log(queryResult);
Filters objects in which a string key’s value starts with the provided text value.
1
2
3
4
5
let query = new Parse.Query('Profile');
// This is faster than other string searches by using backend index
query.startsWith('name', 'Brit');
let queryResult = await query.find();
console.log(queryResult);
Query ordering
Essential in most queries, ordering can be easily achieved in Parse and even chained between two or more ordering constraints.
Sort the results in ascending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
let query = new Parse.Query('Profile');
query.addAscending('friendCount');
let queryResult = await query.find();
console.log(queryResult);
Sort the results in descending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
let query = new Parse.Query('Profile');
query.addDescending('friendCount');
let queryResult = await query.find();
console.log(queryResult);
Sort the results in ascending order, this can be chained without overwriting previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
let query = new Parse.Query('Profile');
query.ascending('friendCount');
let queryResult = await query.find();
console.log(queryResult);
Sort the results in descending order, this can be chained without overwriting previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
let query = new Parse.Query('Profile');
query.descending('friendCount');
let queryResult = await query.find();
console.log(queryResult);
Sorts by text score when using Parse.Query.fullText
.
1
2
3
4
5
let query = new Parse.Query('Profile');
query.fullText('name', 'Dan', { diacriticSensitive: false, caseSensitive: false });
query.sortByTextScore();
let queryResult = await query.find();
console.log(queryResult);
Field selecting
These methods affect which field values can be in your query results.
Return all fields in the returned objects except the ones specified.
1
2
3
4
5
let query = new Parse.Query('Profile');
query.exclude('name');
let queryResult = await query.find();
console.log(queryResult[0].get('name') === undefined);
console.log(queryResult[0].get('birthDay'));
Includes nested Parse.Objects
for the provided key.
1
2
3
4
5
let query = new Parse.Query('Profile');
query.exists('premiumMembership');
query.include('premiumMembership');
let queryResult = await query.find();
console.log(queryResult[0].get('premiumMembership'));
Includes all nested Parse.Objects
.
1
2
3
4
5
let query = new Parse.Query('Profile');
query.exists('premiumMembership');
query.includeAll();
let queryResult = await query.find();
console.log(queryResult[0].get('premiumMembership'));
Return only the specified fields in the returned objects.
1
2
3
4
5
let query = new Parse.Query('Profile');
query.select('name');
let queryResult = await query.find();
console.log(queryResult[0].get('birthDay') === undefined);
console.log(queryResult[0].get('name'));
Geopoint querying
These are methods specific to GeoPoint querying.
Order objects by how near the key value is from the given GeoPoint.
1
2
3
4
let query = new Parse.Query('Profile');
query.near('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
let queryResult = await query.find();
console.log(queryResult);
Find objects whose key value contains the specified GeoPoint.
1
2
3
4
let query = new Parse.Query('Profile');
query.polygonContains('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
let queryResult = await query.find();
console.log(queryResult);
Find objects whose key value is contained within the specified bounding box, composed by two GeoPoint values that set the lower-left and upper-right corner values.
1
2
3
4
let query = new Parse.Query('Profile');
query.withinGeoBox('lastLoginLocation', new Parse.GeoPoint(37.48412167489413, -122.11268034622319), new Parse.GeoPoint(37.28412167489413, -121.91268034622319));
let queryResult = await query.find();
console.log(queryResult);
Find objects whose key value is near the given GeoPoint and within the maxDistance
value. The sorted
boolean value determines if the results should be sorted by distance ascending.
1
2
3
4
let query = new Parse.Query('Profile');
query.withinKilometers('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319), 100, true);
let queryResult = await query.find();
console.log(queryResult);
Find objects whose key value is near the given GeoPoint and within the maxDistance
value. The sorted
boolean value determines if the results should be sorted by distance ascending.
1
2
3
4
let query = new Parse.Query('Profile');
query.withinMiles('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319), 60, true);
let queryResult = await query.find();
console.log(queryResult);
Find objects whose key value is contained within the specified polygon, composed of an array of GeoPoints (at least three). If the polygon path is open, it will be closed automatically by Parse connecting the last and first points.
1
2
3
4
let query = new Parse.Query('Profile');
query.withinPolygon('lastLoginLocation', [new Parse.GeoPoint(37.48412167489413, -122.11268034622319), new Parse.GeoPoint(37.48412167489413, -121.91268034622319), new Parse.GeoPoint(37.28412167489413, -121.91268034622319), new Parse.GeoPoint(37.28412167489413, -122.01268034622319)]);
let queryResult = await query.find();
console.log(queryResult);
Find objects whose key value is near the given GeoPoint and within the maxDistance
value. The sorted
boolean value determines if the results should be sorted by distance ascending.
1
2
3
4
let query = new Parse.Query('Profile');
query.withinRadians('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319), 1.5, true);
let queryResult = await query.find();
console.log(queryResult);
Pagination
These methods are related to pagination utilities, useful for queries that will retrieve a large number of results.
Sets the maximum value of returned results, the default value is 100.
1
2
3
4
let query = new Parse.Query('Profile');
query.limit(2);
let queryResult = await query.find();
console.log(queryResult);
Skips the first n
results in the query, essential for pagination.
1
2
3
4
let query = new Parse.Query('Profile');
query.skip(2);
let queryResult = await query.find();
console.log(queryResult);
Sets a flag that will wrap or not the query response in an object containing results
, holding the array of Parse.Object
and count
integer holding the total number of results.
1
2
3
4
let query = new Parse.Query('Profile');
query.withCount(true);
let queryResult = await query.find();
console.log(queryResult);
Response handling
These methods are helpers for handling the query responses, making it possible to queue callbacks that will be called after your query is resolved. They act as query resolvers as well, like find
and first
.
Iterates over each result from the query and calls a callback for each one, in an unspecified order. Note that execution will halt on a rejected promise, so make sure to handle this case if using promises.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.each((result) => console.log(result));
console.log(queryResult);
Iterates over each result from the query and calls a callback for each batch of results, in an unspecified order. The batchSize
value needs to be passed inside the options
object parameter, being the default 100. Note that execution will halt on a rejected promise, so make sure to handle this case if using promises.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.eachBatch((result) => console.log(result), {batchSize: 2});
console.log(queryResult);
Iterates over each result from the query and calls a callback for each one, in an unspecified order. Note that execution will halt on a rejected promise, so make sure to handle this case if using promises. Differs from each
for passing more parameters down on callback execution.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.filter((currentObject, index, query) => console.log(`${index} - ${currentObject} - ${query}`));
console.log(queryResult);
Iterates over each result from the query and calls a callback for each one, in an unspecified order. Note that execution will halt on a rejected promise, so make sure to handle this case if using promises. Differs from each
for passing more parameters down on callback execution.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.map((currentObject, index, query) => console.log(`${index} - ${currentObject} - ${query}`));
console.log(queryResult);
Iterates over each result from the query and calls a callback for each one, in an unspecified order. Note that execution will halt on a rejected promise, so make sure to handle this case if using promises. Differs from each
for passing more parameters down on callback execution and by allowing direct accumulator handling.
The initialValue
is the value to use as the first argument to the first call of the callback. If no initialValue
is supplied, the first object in the query will be used and skipped.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.reduce((accumulator, currentObject, index) => console.log(`${index} - ${currentObject} - ${accumulator}`));
console.log(queryResult);
Compound query
These methods will create compound queries, which can combine more than one Parse.Query
instance to achieve more complex results.
Helper that is used by Parse to add a constraint that all of passed in queries must match when using Parse.Query.and
.
1
2
3
4
5
6
7
8
let query1 = new Parse.Query('Profile');
query1.greaterThan('friendCount', 10);
let query2 = new Parse.Query('Profile');
query1.lessThan('friendCount', 50);
let query = new Parse.Query('Profile');
query._andQuery([query1, query2]);
let queryResult = await query.find();
console.log(queryResult);
Helper that is used by Parse when using Parse.Query.nor
.
1
2
3
4
5
6
7
8
let query1 = new Parse.Query('Profile');
query1.greaterThan('friendCount', 10);
let query2 = new Parse.Query('Profile');
query1.lessThan('friendCount', 50);
let query = new Parse.Query('Profile');
query._norQuery([query1, query2]);
let queryResult = await query.find();
console.log(queryResult);
Helper that is used by Parse to add a constraint that any of passed in queries must match when using Parse.Query.or
.
1
2
3
4
5
6
7
8
let query1 = new Parse.Query('Profile');
query1.greaterThan('friendCount', 10);
let query2 = new Parse.Query('Profile');
query1.lessThan('friendCount', 50);
let query = new Parse.Query('Profile');
query._orQuery([query1, query2]);
let queryResult = await query.find();
console.log(queryResult);
Compose a compound query that is the AND of the passed queries.
1
2
3
4
5
6
7
let query1 = new Parse.Query('Profile');
query1.greaterThan('friendCount', 10);
let query2 = new Parse.Query('Profile');
query1.lessThan('friendCount', 50);
let query = Parse.Query.and(query1, query2);
let queryResult = await query.find();
console.log(queryResult);
Compose a compound query that is the NOR of the passed queries.
1
2
3
4
5
6
7
let query1 = new Parse.Query('Profile');
query1.greaterThan('friendCount', 10);
let query2 = new Parse.Query('Profile');
query1.lessThan('friendCount', 50);
let query = Parse.Query.nor(query1, query2);
let queryResult = await query.find();
console.log(queryResult);
Compose a compound query that is the OR of the passed queries.
1
2
3
4
5
6
7
let query1 = new Parse.Query('Profile');
query1.greaterThan('friendCount', 10);
let query2 = new Parse.Query('Profile');
query1.lessThan('friendCount', 50);
let query = Parse.Query.or(query1, query2);
let queryResult = await query.find();
console.log(queryResult);
MongoDB related
These methods are related to MongoDB preferences and operations.
Executes an aggregate query, retrieving objects over a set of input values. Please refer to MongoDB documentation on aggregate for better understanding.
1
2
3
let query = new Parse.Query('Profile');
let queryResult = await query.aggregate({ limit: 5 });
console.log(queryResult);
Investigates the query execution plan, related to MongoDB explain operation.
1
2
3
4
let query = new Parse.Query('Profile');
query.explain(true);
let queryResult = await query.find();
console.log(queryResult);
When using a MongoDB replica set, use this method to choose from which replica the objects will be retrieved. The possible values are PRIMARY
(default), PRIMARY_PREFERRED
, SECONDARY
, SECONDARY_PREFERRED
, or NEAREST
.
1
2
3
4
let query = new Parse.Query('Profile');
query.readPreference("PRIMARY");
let queryResult = await query.find();
console.log(queryResult);
Local datastore
These methods enable selecting the source of the queries and using a local datastore.
Changes the source of this query to all pinned objects.
1
2
3
4
5
6
// This should be set before using fromLocalDatastore
Parse.enableLocalDatastore();
let query = new Parse.Query('Profile');
query.fromLocalDatastore();
let queryResult = await query.find();
console.log(queryResult);
Changes the source of this query to your online server.
1
2
3
4
let query = new Parse.Query('Profile');
query.fromNetwork();
let queryResult = await query.find();
console.log(queryResult);
Changes the source of this query to the default group of pinned objects.
1
2
3
4
let query = new Parse.Query('Profile');
query.fromPin();
let queryResult = await query.find();
console.log(queryResult);
Changes the source of this query to a specific group of pinned objects.
1
2
3
4
let query = new Parse.Query('Profile');
query.fromPinWithName('pinnedObjects');
let queryResult = await query.find();
console.log(queryResult);
JSON specifics
Methods that allow queries to be represented as JSON and retrieved.
Returns a JSON representation of this query operations.
1
2
3
4
let query = new Parse.Query('Profile');
query.greaterThan('friendCount', 10);
let queryJSON = await query.toJSON();
console.log(queryJSON);
Add a previously generated JSON representation of query operations to this query.
1
2
3
4
5
6
7
let query = new Parse.Query('Profile');
query.greaterThan('friendCount', 10);
let queryJSON = await query.toJSON();
let query2 = new Parse.Query('Profile');
query2.withJSON(queryJSON);
let queryResult = await query2.find();
console.log(queryResult);
Static method to restore Parse.Query
by JSON representation, internally calling Parse.Query.withJSON
.
1
2
3
4
5
6
let query = new Parse.Query('Profile');
query.greaterThan('friendCount', 10);
let queryJSON = await query.toJSON();
let query2 = Parse.Query.withJSON('Profile', queryJSON);
let queryResult = await query2.find();
console.log(queryResult);
Conclusion
At the end of this guide, you learned how to perform every data query method in Parse. In the next guide, you will learn about complex Parse querying in React Native.