React
Data objects

Query cookbook

22min

React Query Cookbook for Parse

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:

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.

Document image


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 a Membership class containing string name and Date expirationDate fields.

Here is the Parse.Object classes creation code, so go ahead and run it in your API console:

JS


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:

Document image


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.

cancel
count
distinct
find
findAll
first
get


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.

_addCondition
_regexStartWith
containedBy
containedIn

contains
containsAll
containsAllStartingWith
doesNotExist

doesNotMatchKeyInQuery
doesNotMatchQuery
endsWith
equalTo

exists
fullText
greaterThan
greaterThanOrEqualTo

lessThan
lessThanOrEqualTo
matches
matchesKeyInQuery

matchesQuery
notEqualTo
startsWith


Query ordering

Essential in most queries, ordering can be easily achieved in Parse and even chained between two or more ordering constraints.

addAscending
addDescending
ascending
descending
sortByTextScore


Field selecting

These methods affect which field values can be in your query results.

exclude
include
includeAll
select


Geopoint querying

These are methods specific to GeoPoint querying.

near
polygonContains
withinGeoBox
withinKilometers

withinMiles
withinPolygon
withinRadians


Pagination

These methods are related to pagination utilities, useful for queries that will retrieve a large number of results.

limit
skip
withCount


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.

each
eachBatch
filter
map
reduce


Compound query

These methods will create compound queries, which can combine more than one Parse.Query instance to achieve more complex results.

_andQuery
_norQuery
_orQuery
and
nor
or


Database related

These methods are related to the database preferences and operations.

aggregate
explain
readPreference


Local datastore

These methods enable selecting the source of the queries and using a local datastore.

fromLocalDatastore
fromNetwork
fromPin
fromPinWithName


JSON specifics

Methods that allow queries to be represented as JSON and retrieved.

toJSON
withJSON


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 relational Parse querying in React.