Query Cookbook
Parse Query Cookbook in Android
Introduction
We’ve already seen how a ParseQuery with get can retrieve a single ParseQuery from Back4App. There are many other ways to retrieve data withParseQueryyou 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 ParseQuery 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.
You can use Javascript Console on Back4App to create mocked data easily, or you can create your data with your own android app by following this guide.
This tutorial uses an app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion=30
At any time, you can access the complete Android Project built with this tutorial at our Github repositories
Prerequisites
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
Goal
Explore the ParseQuery class different methods and learn query types you can create on Android.
The ParseQuery class
Any query operation on Parse uses the ParseQuery object type, which will help you retrieve specific data from your Back4App throughout your app. To create a new ParseQuery, you need to pass as a parameter the desired ParseQuery subclass, which is the one that will contain your query results.
It is crucial to know that a ParseQuery will only resolve after calling a retrieve method (like ParseQuery.find or ParseQuery.get), so a query can be set up and several modifiers can be chained before actually being called.
You can read more about the ParseQuery 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
- Date type
- Number (integer) type
- Array (string array) type
- Array (Number array) type
- GeoPoint type
- Nullable pointer type
Here is the Parse.Object classes creation code, so go ahead and run it in your API console:
1 // Add Profile objects and create table
2 // Adam Sandler
3 let Profile = new Parse.Object('Profile');
4 Profile.set('name', 'Adam Sandler');
5 Profile.set('birthDay', new Date('09/09/1966'));
6 Profile.set('friendCount', 2);
7 Profile.set('favoriteFoods', ['Lobster', 'Bread']);
8 Profile.set('luckyNumbers', [2, 7]);
9 Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
10 await Profile.save();
11
12 // Britney Spears
13 Profile = new Parse.Object('Profile');
14 Profile.set('name', 'Britney Spears');
15 Profile.set('birthDay', new Date('12/02/1981'));
16 Profile.set('friendCount', 52);
17 Profile.set('favoriteFoods', ['Cake', 'Bread']);
18 Profile.set('luckyNumbers', [22, 7]);
19 Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
20 await Profile.save();
21
22 // Carson Kressley
23 Profile = new Parse.Object('Profile');
24 Profile.set('name', 'Carson Kressley');
25 Profile.set('birthDay', new Date('11/11/1969'));
26 Profile.set('friendCount', 12);
27 Profile.set('favoriteFoods', ['Fish', 'Cookies']);
28 Profile.set('luckyNumbers', [8, 2]);
29 Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
30 await Profile.save();
31
32 // Dan Aykroyd
33 // Creates related object Membership for this user only
34 let Membership = new Parse.Object('Membership');
35 Membership.set('name', 'Premium');
36 Membership.set('expirationDate', new Date('10/10/2030'))
37 await Membership.save();
38 Profile = new Parse.Object('Profile');
39 Profile.set('name', 'Dan Aykroyd');
40 Profile.set('birthDay', new Date('07/01/1952'));
41 Profile.set('friendCount', 66);
42 Profile.set('favoriteFoods', ['Jam', 'Peanut Butter']);
43 Profile.set('luckyNumbers', [22, 77]);
44 Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
45 Profile.set('premiumMembership', Membership);
46 await Profile.save();
47
48 // Eddie Murphy
49 Profile = new Parse.Object('Profile');
50 Profile.set('name', 'Eddie Murphy');
51 Profile.set('birthDay', new Date('04/03/1961'));
52 Profile.set('friendCount', 49);
53 Profile.set('favoriteFoods', ['Lettuce', 'Pepper']);
54 Profile.set('luckyNumbers', [6, 5]);
55 Profile.set('lastLoginLocation', new Parse.GeoPoint(-27.104919974838154, -52.61428045237739));
56 await Profile.save();
57
58 // Fergie
59 Profile = new Parse.Object('Profile');
60 Profile.set('name', 'Fergie');
61 Profile.set('birthDay', new Date('03/27/1975'));
62 Profile.set('friendCount', 55);
63 Profile.set('favoriteFoods', ['Lobster', 'Shrimp']);
64 Profile.set('luckyNumbers', [13, 7]);
65 Profile.set('lastLoginLocation', new Parse.GeoPoint(-27.104919974838154, -52.61428045237739));
66 await Profile.save();
67
68 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 ParseQuery 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.
This is the java methods:
//Cancels the current network request.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.findInBackground();
3 query.cancel();This is the kotlin methods:
//Cancels the current network request.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.findInBackground()
3 query.cancel()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.
These are Java methods
//Filters objects in which a key value is contained in the provided array of values.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.whereContainedIn("luckyNumbers", List.of(2, 7));
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9
10 });These are Kotlin methods
//Filters objects in which a key value is contained in the provided array of values.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.whereContainedIn("luckyNumbers", java.util.List.of(2, 7))
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }These are Java methods
//Requires that a key’s value does not match a value in an object returned by a different ParseQuery. Useful for multi-object querying.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 ParseQuery<ParseObject> innerQuery = new ParseQuery<>("Profile");
3 innerQuery.whereLessThan("friendCount", 50);
4 query.whereDoesNotMatchKeyInQuery("friendCount", "friendCount", innerQuery);
5 query.whereGreaterThan("friendCount", 10);
6 query.findInBackground((objects, e) -> {
7 if (e == null) {
8 Log.d(TAG, "Objects: " + objects);
9 } else {
10 Log.e(TAG, "Parse Error: ", e);
11 }
12 });//Filters objects in which a specific key’s value is greater than or equal to the provided value.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.whereGreaterThanOrEqualTo("friendCount", 49);
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9 });These are Kotlin methods
//Filters objects in which a specific key’s value is equal to the provided value.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.whereEqualTo("friendCount", 2)
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }//Filters objects in which a key value is set.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.whereExists("premiumMembership")
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }These are Java methods
//Filters objects in which a string key’s value starts with the provided text value.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.whereStartsWith("name", "Brit");
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9 });These are Kotlin methods
//Filters objects in which a string key’s value starts with the provided text value.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.whereStartsWith("name", "Brit")
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }Query ordering
Essential in most queries, ordering can be easily achieved in Parse and even chained between two or more ordering constraints.
These are Java methods
//Sort the results in descending order, this can be chained without overwriting previous orderings. Multiple keys can be used to solve ordering ties.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.orderByDescending("friendCount");
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9
10 });These are Kotlin methods
//Sort the results in ascending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.addAscendingOrder("friendCount")
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }Field selecting
These methods affect which field values can be in your query results.
These are Java methods
//Return all fields in the returned objects except the ones specified.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.whereExists("premiumMembership");
3 query.include("premiumMembership");
4 query.findInBackground((objects, e) -> {
5 if (e == null) {
6 Log.d(TAG, "Objects: " + objects);
7 Log.d(TAG, "Object Premium Membership: " + objects.get(0).get("premiumMembership"));
8 } else {
9 Log.e(TAG, "Parse Error: ", e);
10 }
11
12 });These are Kotlin methods
//Return all fields in the returned objects except the ones specified.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.whereExists("premiumMembership")
3 query.include("premiumMembership")
4 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
5 if (e == null) {
6 Log.d(Companion.TAG, "Objects: $objects")
7 Log.d(
8 Companion.TAG,
9 "Object Premium Membership: " + objects[0]["premiumMembership"]
10 )
11 } else {
12 Log.e(Companion.TAG, "Parse Error: ", e)
13 }
14 }Geopoint querying
These are methods specific to GeoPoint querying.
These are Java Methods
//Order objects by how near the key value is from the given GeoPoint.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.whereNear("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319));
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9
10 });//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 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.whereWithinMiles("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319), 100);
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9
10 });These are Kotlin methods
//Order objects by how near the key value is from the given GeoPoint.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.whereNear("lastLoginLocation", ParseGeoPoint(37.38412167489413, -122.01268034622319))
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }//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 val query = ParseQuery<ParseObject>("Profile")
2 query.whereWithinMiles(
3 "lastLoginLocation",
4 ParseGeoPoint(37.38412167489413, -122.01268034622319),
5 100.0
6 )
7 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
8 if (e == null) {
9 Log.d(Companion.TAG, "Objects: $objects")
10 } else {
11 Log.e(Companion.TAG, "Parse Error: ", e)
12 }
13 }Pagination
These methods are related to pagination utilities, useful for queries that will retrieve a large number of results.
These are Java methods
//Sets the maximum value of returned results, the default value is 100.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.setLimit(2);
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9
10 });These are Kotlin methods
//Sets the maximum value of returned results, the default value is 100.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.limit = 2
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }Local datastore
These methods enable selecting the source of the queries and using a local datastore.
These are Java methods
//Changes the source of this query to a specific group of pinned objects.
1 ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
2 query.fromPin("pinnedObjects");
3 query.findInBackground((objects, e) -> {
4 if (e == null) {
5 Log.d(TAG, "Objects: " + objects);
6 } else {
7 Log.e(TAG, "Parse Error: ", e);
8 }
9 });These are Kotlin methods
//Changes the source of this query to a specific group of pinned objects.
1 val query = ParseQuery<ParseObject>("Profile")
2 query.fromPin("pinnedObjects")
3 query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
4 if (e == null) {
5 Log.d(Companion.TAG, "Objects: $objects")
6 } else {
7 Log.e(Companion.TAG, "Parse Error: ", e)
8 }
9 }Conclusion
At the end of this guide, you learned how to use different query types in Android.