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 with ParseQuery
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 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 Project via our GitHub repositories.
Prerequisites
To complete this tutorial, we need:
- Android Studio.
- An app created on Back4App.
- Note: Follow the New Parse App tutorial to learn how to create a Parse App on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
- A device (or virtual device) running Android 4.1 (Jelly Bean) or newer.
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
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 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
2
3
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.findInBackground();
query.cancel();
Retrieves the count of ParseObject
results that meet the query criteria.
1
2
3
4
5
6
7
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
try {
int queryCount = query.count();
Log.d(TAG, "Count: " + queryCount);
} catch (com.parse.ParseException parseException) {
parseException.printStackTrace();
}
This is the basic method for retrieving your query results, always returning an array of ParseObject
instances, being empty when none are found.
1
2
3
4
5
6
7
8
//This find function works synchronously.
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
try {
List<ParseObject> list = query.find();
Log.d(TAG, "List: " + list);
} catch (com.parse.ParseException e) {
e.printStackTrace();
}
This is the basic method for retrieving your query results, always returning an array of ParseObject
instances, being empty when none are found.
1
2
3
4
5
6
7
8
9
//This find function works asynchronously.
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "ParseError: ", e);
}
});
Retrieves the first ParseObject
instance that meets the query criteria.
1
2
3
4
5
6
7
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
try {
ParseObject firstItem = query.getFirst();
Log.d(TAG, "First Item: " + firstItem);
} catch (ParseException e) {
e.printStackTrace();
}
This quick method is used to retrieve a single ParseObject
instance when you know its objectId.
1
2
3
4
5
6
7
8
//We can call a parse object with an object id with the get() function.
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
try {
ParseObject object = query.get("C6ENdLnFdQ");
Log.d(TAG, "Object: " + object);
} catch (ParseException e) {
e.printStackTrace();
}
This is the kotlin methods:
Cancels the current network request.
1
2
3
val query = ParseQuery<ParseObject>("Profile")
query.findInBackground()
query.cancel()
Retrieves the count of ParseObject
results that meet the query criteria.
1
2
3
4
5
6
7
val query = ParseQuery<ParseObject>("Profile")
try {
val queryCount = query.count()
Log.d(Companion.TAG, "Count: $queryCount")
} catch (parseException: ParseException) {
parseException.printStackTrace()
}
This is the basic method for retrieving your query results, always returning an array of ParseObject
instances, being empty when none are found.
1
2
3
4
5
6
7
8
//This find function works synchronously.
val query = ParseQuery<ParseObject>("Profile")
try {
val list = query.find()
Log.d(Companion.TAG, "List: $list")
} catch (e: ParseException) {
e.printStackTrace()
}
This is the basic method for retrieving your query results, always returning an array of ParseObject
instances, being empty when none are found.
1
2
3
4
5
6
7
8
9
//This find function works asynchronously.
val query = ParseQuery<ParseObject>("Profile")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "ParseError: ", e)
}
}
Retrieves the first ParseObject
instance that meets the query criteria.
1
2
3
4
5
6
7
val query = ParseQuery<ParseObject>("Profile")
try {
val firstItem = query.first
Log.d(Companion.TAG, "First Item: $firstItem")
} catch (e: ParseException) {
e.printStackTrace()
}
This quick method is used to retrieve a single ParseObject
instance when you know its objectId.
1
2
3
4
5
6
7
8
//We can call a parse object with an object id with the get() function.
val query = ParseQuery<ParseObject>("Profile")
try {
val `object` = query["C6ENdLnFdQ"]
Log.d(Companion.TAG, "Object: $`object`")
} catch (e: ParseException) {
e.printStackTrace()
}
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
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereContainedIn("luckyNumbers", List.of(2, 7));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereContains("name", "da");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which an array type key value must contain every value provided.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereContainsAll("luckyNumbers", List.of(2, 7));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which an array type key value must contain every string value provided.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereContainsAllStartsWith("favoriteFoods", List.of("Shrimp", "Lobster"));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a key value is not set.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereDoesNotExist("premiumMembership");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin methods
Filters objects in which a key value is contained in the provided array of values.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereContainedIn("luckyNumbers", java.util.List.of(2, 7))
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereContains("name", "da")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which an array type key value must contain every value provided.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereContainsAll("luckyNumbers", java.util.List.of(2, 7))
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which an array type key value must contain every string value provided.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereContainsAllStartsWith("favoriteFoods", java.util.List.of("Shrimp", "Lobster"))
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a key value is not set.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereDoesNotExist("premiumMembership")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
2
3
4
5
6
7
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
ParseQuery<ParseObject> innerQuery = new ParseQuery<>("Profile");
innerQuery.whereLessThan("friendCount", 50);
query.whereDoesNotMatchKeyInQuery("friendCount", "friendCount", innerQuery);
query.whereGreaterThan("friendCount", 10);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
ParseQuery<ParseObject> innerQuery = new ParseQuery<>("Membership");
innerQuery.whereGreaterThan("expirationDate", new Date());
query.whereExists("premiumMembership");
query.whereDoesNotMatchQuery("premiumMembership", innerQuery);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a string key’s value ends with the provided text value.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereEndsWith("name", "ie");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a specific key’s value is equal to the provided value.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereEqualTo("friendCount", 2);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a key value is set.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereExists("premiumMembership");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereFullText("name", "Spears");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a specific key’s value is greater than the provided value.
1
2
3
4
5
6
7
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
Calendar calendar = Calendar.getInstance();
calendar.set(1980, 8, 19, 59, 59, 59);
Date date = calendar.getTime();
query.whereGreaterThan("birthDay", date);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a specific key’s value is greater than or equal to the provided value.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereGreaterThanOrEqualTo("friendCount", 49);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin 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
2
3
4
5
6
7
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
val innerQuery = ParseQuery<ParseObject>("Profile")
innerQuery.whereLessThan("friendCount", 50)
query.whereDoesNotMatchKeyInQuery("friendCount", "friendCount", innerQuery)
query.whereGreaterThan("friendCount", 10)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
val innerQuery = ParseQuery<ParseObject>("Membership")
innerQuery.whereGreaterThan("expirationDate", Date())
query.whereExists("premiumMembership")
query.whereDoesNotMatchQuery("premiumMembership", innerQuery)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a string key’s value ends with the provided text value.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereEndsWith("name", "ie")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a specific key’s value is equal to the provided value.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereEqualTo("friendCount", 2)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a key value is set.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereExists("premiumMembership")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereFullText("name", "Spears")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a specific key’s value is greater than the provided value.
1
2
3
4
5
6
7
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
val calendar = Calendar.getInstance()
calendar[1980, 8, 19, 59, 59] = 59
val date = calendar.time
query.whereGreaterThan("birthDay", date)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a specific key’s value is greater than or equal to the provided value.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereGreaterThanOrEqualTo("friendCount", 49)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
These are Java methods
Filters objects in which a specific key’s value is lesser than the provided value.
1
2
3
4
5
6
7
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
Calendar calendar = Calendar.getInstance();
calendar.set(1980, 8, 19, 59, 59, 59);
Date date = calendar.getTime();
query.whereLessThan("birthDay", date);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a specific key’s value is lesser than or equal to the provided value.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereLessThanOrEqualTo("friendCount", 49);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a string type key value must match the provided regular expression and its modifiers.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereMatches("name", "da", "i");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Requires that a key’s value matches a value in an object returned by a different ParseQuery. Useful for multi-object querying.
1
2
3
4
5
6
7
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
ParseQuery<ParseObject> innerQuery = new ParseQuery<>("Profile");
innerQuery.whereLessThan("friendCount", 50);
query.whereMatchesKeyInQuery("friendCount", "friendCount", innerQuery);
query.whereGreaterThan("friendCount", 10);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Requires that an object contained in the given key matches another query. Useful for multi-object querying.
1
2
3
4
5
6
7
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
ParseQuery<ParseObject> innerQuery = new ParseQuery<>("Membership");
innerQuery.whereGreaterThan("expirationDate", new Date());
query.whereExists("premiumMembership");
query.whereMatchesQuery("premiumMembership", innerQuery);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a specific key’s value is not equal to the provided value.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereNotEqualTo("friendCount", 2);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Filters objects in which a string key’s value starts with the provided text value.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereStartsWith("name", "Brit");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin methods
Filters objects in which a specific key’s value is lesser than the provided value.
1
2
3
4
5
6
7
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
val calendar = Calendar.getInstance()
calendar[1980, 8, 19, 59, 59] = 59
val date = calendar.time
query.whereLessThan("birthDay", date)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a specific key’s value is lesser than or equal to the provided value.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereLessThanOrEqualTo("friendCount", 49)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a string type key value must match the provided regular expression and its modifiers.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereMatches("name", "da", "i")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Requires that a key’s value matches a value in an object returned by a different ParseQuery. Useful for multi-object querying.
1
2
3
4
5
6
7
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
val innerQuery = ParseQuery<ParseObject>("Profile")
innerQuery.whereLessThan("friendCount", 50)
query.whereMatchesKeyInQuery("friendCount", "friendCount", innerQuery)
query.whereGreaterThan("friendCount", 10)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Requires that an object contained in the given key matches another query. Useful for multi-object querying.
1
2
3
4
5
6
7
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
val innerQuery = ParseQuery<ParseObject>("Membership")
innerQuery.whereGreaterThan("expirationDate", Date())
query.whereExists("premiumMembership")
query.whereMatchesQuery("premiumMembership", innerQuery)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a specific key’s value is not equal to the provided value.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereNotEqualTo("friendCount", 2)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Filters objects in which a string key’s value starts with the provided text value.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereStartsWith("name", "Brit")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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 ascending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.addAscendingOrder("friendCount");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Sort the results in descending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.addDescendingOrder("friendCount");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.orderByAscending("friendCount");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.orderByDescending("friendCount");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin methods
Sort the results in ascending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.addAscendingOrder("friendCount")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Sort the results in descending order, overwrites previous orderings. Multiple keys can be used to solve ordering ties.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.addDescendingOrder("friendCount")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.orderByAscending("friendCount")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.orderByDescending("friendCount")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
2
3
4
5
6
7
8
9
10
11
12
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereExists("premiumMembership");
query.include("premiumMembership");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
Log.d(TAG, "Object Premium Membership: " + objects.get(0).get("premiumMembership"));
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Return only the specified fields in the returned objects.
1
2
3
4
5
6
7
8
9
10
11
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.selectKeys(List.of("name"));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
Log.d(TAG, "Object name: " + objects.get(0).get("name"));
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin methods
Return all fields in the returned objects except the ones specified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
val query = ParseQuery<ParseObject>("Profile")
query.whereExists("premiumMembership")
query.include("premiumMembership")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
Log.d(
Companion.TAG,
"Object Premium Membership: " + objects[0]["premiumMembership"]
)
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Return only the specified fields in the returned objects.
1
2
3
4
5
6
7
8
9
10
val query = ParseQuery<ParseObject>("Profile")
query.selectKeys(java.util.List.of("name"))
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
Log.d(Companion.TAG, "Object name: " + objects[0]["name"])
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereNear("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Find objects whose key value contains the specified GeoPoint.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.wherePolygonContains("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereWithinGeoBox("lastLoginLocation", new ParseGeoPoint(37.48412167489413, -122.11268034622319), new ParseGeoPoint(37.28412167489413, -121.91268034622319));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereWithinKilometers("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319), 100);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereWithinMiles("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319), 100);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
11
12
13
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereWithinPolygon("lastLoginLocation", List.of(new ParseGeoPoint(37.48412167489413, -122.11268034622319),
new ParseGeoPoint(37.48412167489413, -121.91268034622319),
new ParseGeoPoint(37.28412167489413, -121.91268034622319),
new ParseGeoPoint(37.28412167489413, -122.01268034622319)));
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
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
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereWithinRadians("lastLoginLocation", new ParseGeoPoint(37.38412167489413, -122.01268034622319), 100);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin Methods
Order objects by how near the key value is from the given GeoPoint.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.whereNear("lastLoginLocation", ParseGeoPoint(37.38412167489413, -122.01268034622319))
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Find objects whose key value contains the specified GeoPoint.
1
2
3
4
5
6
7
8
9
10
11
12
val query = ParseQuery<ParseObject>("Profile")
query.wherePolygonContains(
"lastLoginLocation",
ParseGeoPoint(37.38412167489413, -122.01268034622319)
)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
10
11
12
13
val query = ParseQuery<ParseObject>("Profile")
query.whereWithinGeoBox(
"lastLoginLocation",
ParseGeoPoint(37.48412167489413, -122.11268034622319),
ParseGeoPoint(37.28412167489413, -121.91268034622319)
)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
10
11
12
13
val query = ParseQuery<ParseObject>("Profile")
query.whereWithinKilometers(
"lastLoginLocation",
ParseGeoPoint(37.38412167489413, -122.01268034622319),
100.0
)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
10
11
12
13
val query = ParseQuery<ParseObject>("Profile")
query.whereWithinMiles(
"lastLoginLocation",
ParseGeoPoint(37.38412167489413, -122.01268034622319),
100.0
)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
10
11
12
13
14
15
16
val query = ParseQuery<ParseObject>("Profile")
query.whereWithinPolygon(
"lastLoginLocation", java.util.List.of(
ParseGeoPoint(37.48412167489413, -122.11268034622319),
ParseGeoPoint(37.48412167489413, -121.91268034622319),
ParseGeoPoint(37.28412167489413, -121.91268034622319),
ParseGeoPoint(37.28412167489413, -122.01268034622319)
)
)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
5
6
7
8
9
10
11
12
13
val query = ParseQuery<ParseObject>("Profile")
query.whereWithinRadians(
"lastLoginLocation",
ParseGeoPoint(37.38412167489413, -122.01268034622319),
100.0
)
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.setLimit(2);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Skips the first n
results in the query, essential for pagination.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.setSkip(2);
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin methods
Sets the maximum value of returned results, the default value is 100.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.limit = 2
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Skips the first n
results in the query, essential for pagination.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.skip = 2
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
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 all pinned objects.
1
2
3
4
5
6
7
8
9
10
11
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
//If you want use LocalDataStore you should enable local data store in the App.java or App.kt file.
query.fromLocalDatastore();
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Changes the source of this query to your online server.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.fromNetwork();
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Changes the source of this query to the default group of pinned objects.
1
2
3
4
5
6
7
8
9
10
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.fromPin();
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
Changes the source of this query to a specific group of pinned objects.
1
2
3
4
5
6
7
8
9
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.fromPin("pinnedObjects");
query.findInBackground((objects, e) -> {
if (e == null) {
Log.d(TAG, "Objects: " + objects);
} else {
Log.e(TAG, "Parse Error: ", e);
}
});
These are Kotlin methods
Changes the source of this query to all pinned objects.
1
2
3
4
5
6
7
8
9
10
val query = ParseQuery<ParseObject>("Profile")
//If you want use LocalDataStore you should enable local data store in the App.java or App.kt file.
query.fromLocalDatastore()
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Changes the source of this query to your online server.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.fromNetwork()
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Changes the source of this query to the default group of pinned objects.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.fromPin()
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Changes the source of this query to a specific group of pinned objects.
1
2
3
4
5
6
7
8
9
val query = ParseQuery<ParseObject>("Profile")
query.fromPin("pinnedObjects")
query.findInBackground { objects: List<ParseObject>, e: ParseException? ->
if (e == null) {
Log.d(Companion.TAG, "Objects: $objects")
} else {
Log.e(Companion.TAG, "Parse Error: ", e)
}
}
Conclusion
At the end of this guide, you learned how to use different query types in Android.