Class Query (3.7.3)

Stay organized with collections Save and categorize content based on your preferences.
public class Query

A Query which you can read or listen to. You can also construct refined Query objects by adding filters and ordering.

Inheritance

Object > Query

Static Methods

fromProto(Firestore firestore, RunQueryRequest proto)

public static Query fromProto(Firestore firestore, RunQueryRequest proto)

Returns a Query instance that can be used to execute the provided RunQueryRequest.

Only RunQueryRequests that pertain to the same project as the Firestore instance can be deserialized.

Runtime metadata (as required for limitToLast() queries) is not restored and as such, the results for limitToLast() queries will be returned in the original backend order.

Parameters
NameDescription
firestoreFirestore

a Firestore instance to apply the query to

protoRunQueryRequest

the serialized RunQueryRequest

Returns
TypeDescription
Query

a Query instance that can be used to execute the RunQueryRequest

Constructors

Query(FirestoreRpcContext<?> rpcContext, Query.QueryOptions queryOptions)

protected Query(FirestoreRpcContext<?> rpcContext, Query.QueryOptions queryOptions)
Parameters
NameDescription
rpcContextcom.google.cloud.firestore.FirestoreRpcContext<?>
queryOptionscom.google.cloud.firestore.Query.QueryOptions

Methods

addSnapshotListener(EventListener<QuerySnapshot> listener)

public ListenerRegistration addSnapshotListener(EventListener<QuerySnapshot> listener)

Starts listening to this query.

Parameter
NameDescription
listenerEventListener<QuerySnapshot>

The event listener that will be called with the snapshots.

Returns
TypeDescription
ListenerRegistration

A registration object that can be used to remove the listener.

addSnapshotListener(Executor executor, EventListener<QuerySnapshot> listener)

public ListenerRegistration addSnapshotListener(Executor executor, EventListener<QuerySnapshot> listener)

Starts listening to this query.

Parameters
NameDescription
executorExecutor

The executor to use to call the listener.

listenerEventListener<QuerySnapshot>

The event listener that will be called with the snapshots.

Returns
TypeDescription
ListenerRegistration

A registration object that can be used to remove the listener.

count()

public AggregateQuery count()

Returns a query that counts the documents in the result set of this query.

The returned query, when executed, counts the documents in the result set of this query without actually downloading the documents.

Using the returned query to count the documents is efficient because only the final count, not the documents' data, is downloaded. The returned query can even count the documents if the result set would be prohibitively large to download entirely (e.g. thousands of documents).

Returns
TypeDescription
AggregateQuery

a query that counts the documents in the result set of this query.

endAt(DocumentSnapshot snapshot)

public Query endAt(DocumentSnapshot snapshot)

Creates and returns a new Query that ends at the provided document (inclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

Parameter
NameDescription
snapshotDocumentSnapshot

The snapshot of the document to end at.

Returns
TypeDescription
Query

The created Query.

endAt(Object[] fieldValues)

public Query endAt(Object[] fieldValues)

Creates and returns a new Query that ends at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

Parameter
NameDescription
fieldValuesObject[]

The field values to end this query at, in order of the query's order by.

Returns
TypeDescription
Query

The created Query.

endBefore(DocumentSnapshot snapshot)

public Query endBefore(DocumentSnapshot snapshot)

Creates and returns a new Query that ends before the provided document (exclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

Parameter
NameDescription
snapshotDocumentSnapshot

The snapshot of the document to end before.

Returns
TypeDescription
Query

The created Query.

endBefore(Object[] fieldValues)

public Query endBefore(Object[] fieldValues)

Creates and returns a new Query that ends before the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

Parameter
NameDescription
fieldValuesObject[]

The field values to end this query before, in order of the query's order by.

Returns
TypeDescription
Query

The created Query.

equals(Object obj)

public boolean equals(Object obj)

Returns true if this Query is equal to the provided object.

Parameter
NameDescription
objObject

The object to compare against.

Returns
TypeDescription
boolean

Whether this Query is equal to the provided object.

Overrides

get()

public ApiFuture<QuerySnapshot> get()

Executes the query and returns the results as QuerySnapshot.

Returns
TypeDescription
ApiFuture<QuerySnapshot>

An ApiFuture that will be resolved with the results of the Query.

getFirestore()

public Firestore getFirestore()

Gets the Firestore instance associated with this query.

Returns
TypeDescription
Firestore

The Firestore instance associated with this query.