Class Query

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

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.

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

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

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.

hashCode()

public int hashCode()
Returns
TypeDescription
int
Overrides

limit(int limit)

public Query limit(int limit)

Creates and returns a new Query that only returns the first matching documents.

Parameter
NameDescription
limitint

The maximum number of items to return.

Returns
TypeDescription
Query

The created Query.

limitToLast(int limit)

public Query limitToLast(int limit)

Creates and returns a new Query that only returns the last matching documents.

You must specify at least one orderBy clause for limitToLast queries. Otherwise, an java.lang.IllegalStateException is thrown during execution.

Results for limitToLast() queries are only available once all documents are received. Hence, limitToLast() queries cannot be streamed via the #stream(ApiStreamObserver) API.

Parameter
NameDescription
limitint

the maximum number of items to return

Returns
TypeDescription
Query

the created Query

offset(int offset)

public Query offset(int offset)

Creates and returns a new Query that skips the first n results.

Parameter
NameDescription
offsetint

The number of items to skip.

Returns
TypeDescription
Query

The created Query.

orderBy(FieldPath fieldPath)

public Query orderBy(FieldPath fieldPath)

Creates and returns a new Query that's additionally sorted by the specified field.

Parameter
NameDescription
fieldPathFieldPath

The field to sort by.

Returns
TypeDescription
Query

The created Query.

orderBy(FieldPath fieldPath, Query.Direction direction)

public Query orderBy(FieldPath fieldPath, Query.Direction direction)

Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.

Parameters
NameDescription
fieldPathFieldPath

The field to sort by.

directionQuery.Direction

The direction to sort.

Returns
TypeDescription
Query

The created Query.

orderBy(String field)

public Query orderBy(String field)

Creates and returns a new Query that's additionally sorted by the specified field.

Parameter
NameDescription
fieldString

The field to sort by.

Returns
TypeDescription
Query

The created Query.

orderBy(String field, Query.Direction direction)

public Query orderBy(String field, Query.Direction direction)

Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.

Parameters
NameDescription
fieldString

The field to sort by.

directionQuery.Direction

The direction to sort.

Returns
TypeDescription
Query

The created Query.

select(FieldPath[] fieldPaths)

public Query select(FieldPath[] fieldPaths)

Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.

Parameter
NameDescription
fieldPathsFieldPath[]

The field paths to include.

Returns
TypeDescription
Query

The created Query.

select(String[] fields)

public Query select(String[] fields)

Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.

Parameter
NameDescription
fieldsString[]

The fields to include.

Returns
TypeDescription
Query

The created Query.

startAfter(DocumentSnapshot snapshot)

public Query startAfter(DocumentSnapshot snapshot)

Creates and returns a new Query that starts after the provided document (exclusive). The starting 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 start after.

Returns
TypeDescription
Query

The created Query.

startAfter(Object[] fieldValues)

public Query startAfter(Object[] fieldValues)

Creates and returns a new Query that starts after 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 start this query after, in order of the query's order by.

Returns
TypeDescription
Query

The created Query.

startAt(DocumentSnapshot snapshot)

public Query startAt(DocumentSnapshot snapshot)

Creates and returns a new Query that starts at the provided document (inclusive). The starting 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 start at.

Returns
TypeDescription
Query

The created Query.

startAt(Object[] fieldValues)

public Query startAt(Object[] fieldValues)

Creates and returns a new Query that starts 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 start this query at, in order of the query's order by.

Returns
TypeDescription
Query

The created Query.

stream(ApiStreamObserver<DocumentSnapshot> responseObserver)

public void stream(ApiStreamObserver<DocumentSnapshot> responseObserver)

Executes the query and streams the results as a StreamObserver of DocumentSnapshots.

Parameter
NameDescription
responseObserverApiStreamObserver<DocumentSnapshot>

The observer to be notified when results arrive.

toProto()

public RunQueryRequest toProto()

Returns the RunQueryRequest that this Query instance represents. The request contains the serialized form of all Query constraints.

Runtime metadata (as required for limitToLast() queries) is not serialized and as such, the serialized request will return the results in the original backend order.

Returns
TypeDescription
RunQueryRequest

the serialized RunQueryRequest

whereArrayContains(FieldPath fieldPath, Object value)

public Query whereArrayContains(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.

A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().

Parameters
NameDescription
fieldPathFieldPath

The path of the field containing an array to search

valueObject

The value that must be contained in the array

Returns
TypeDescription
Query

The created Query.

whereArrayContains(String field, Object value)

public Query whereArrayContains(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.

A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().

Parameters
NameDescription
fieldString

The name of the field containing an array to search

valueObject

The value that must be contained in the array

Returns
TypeDescription
Query

The created Query.

whereArrayContainsAny(FieldPath fieldPath, List<? extends Object> values)

public Query whereArrayContainsAny(FieldPath fieldPath, List<? extends Object> values)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.

A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().

Parameters
NameDescription
fieldPathFieldPath

The path of the field containing an array to search.

valuesList<? extends java.lang.Object>

A list that contains the values to match.

Returns
TypeDescription
Query

The created Query.

whereArrayContainsAny(String field, List<? extends Object> values)

public Query whereArrayContainsAny(String field, List<? extends Object> values)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.

A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().

Parameters
NameDescription
fieldString

The name of the field containing an array to search.

valuesList<? extends java.lang.Object>

A list that contains the values to match.

Returns
TypeDescription
Query

The created Query.

whereEqualTo(FieldPath fieldPath, Object value)

public Query whereEqualTo(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.

Parameters
NameDescription
fieldPathFieldPath

The path of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereEqualTo(String field, Object value)

public Query whereEqualTo(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.

Parameters
NameDescription
fieldString

The name of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereGreaterThan(FieldPath fieldPath, Object value)

public Query whereGreaterThan(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.

Parameters
NameDescription
fieldPathFieldPath

The path of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereGreaterThan(String field, Object value)

public Query whereGreaterThan(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.

Parameters
NameDescription
fieldString

The name of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereGreaterThanOrEqualTo(FieldPath fieldPath, Object value)

public Query whereGreaterThanOrEqualTo(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.

Parameters
NameDescription
fieldPathFieldPath

The path of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereGreaterThanOrEqualTo(String field, Object value)

public Query whereGreaterThanOrEqualTo(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.

Parameters
NameDescription
fieldString

The name of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereIn(FieldPath fieldPath, List<? extends Object> values)

public Query whereIn(FieldPath fieldPath, List<? extends Object> values)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.

A Query can have only one whereIn() filter, and it cannot be combined with whereArrayContainsAny().

Parameters
NameDescription
fieldPathFieldPath

The path of the field to search.

valuesList<? extends java.lang.Object>

A list that contains the values to match.

Returns
TypeDescription
Query

The created Query.

whereIn(String field, List<? extends Object> values)

public Query whereIn(String field, List<? extends Object> values)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.

A Query can have only one whereIn() filter, and it cannot be combined with whereArrayContainsAny().

Parameters
NameDescription
fieldString

The name of the field to search.

valuesList<? extends java.lang.Object>

A list that contains the values to match.

Returns
TypeDescription
Query

The created Query.

whereLessThan(FieldPath fieldPath, Object value)

public Query whereLessThan(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.

Parameters
NameDescription
fieldPathFieldPath

The path of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereLessThan(String field, Object value)

public Query whereLessThan(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.

Parameters
NameDescription
fieldString

The name of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereLessThanOrEqualTo(FieldPath fieldPath, Object value)

public Query whereLessThanOrEqualTo(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less or equal to the specified value.

Parameters
NameDescription
fieldPathFieldPath

The path of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereLessThanOrEqualTo(String field, Object value)

public Query whereLessThanOrEqualTo(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less or equal to the specified value.

Parameters
NameDescription
fieldString

The name of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereNotEqualTo(FieldPath fieldPath, Object value)

public Query whereNotEqualTo(FieldPath fieldPath, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal the specified value.

Parameters
NameDescription
fieldPathFieldPath

The path of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereNotEqualTo(String field, Object value)

public Query whereNotEqualTo(String field, Object value)

Creates and returns a new Query with the additional filter that documents must contain the specified field and its value does not equal the specified value.

Parameters
NameDescription
fieldString

The name of the field to compare.

valueObject

The value for comparison.

Returns
TypeDescription
Query

The created Query.

whereNotIn(FieldPath fieldPath, List<? extends Object> values)

public Query whereNotIn(FieldPath fieldPath, List<? extends Object> values)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.

A Query can have only one whereNotIn() filter, and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().

Parameters
NameDescription
fieldPathFieldPath

The path of the field to search.

valuesList<? extends java.lang.Object>

The list that contains the values to match.

Returns
TypeDescription
Query

The created Query.

whereNotIn(String field, List<? extends Object> values)

public Query whereNotIn(String field, List<? extends Object> values)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.

A Query can have only one whereNotIn() filter and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().

Parameters
NameDescription
fieldString

The name of the field to search.

valuesList<? extends java.lang.Object>

The list that contains the values to match.

Returns
TypeDescription
Query

The created Query.