public class Query
A Query which you can read or listen to. You can also construct refined Query objects by adding filters and ordering.
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.
Name | Description |
firestore | Firestore a Firestore instance to apply the query to |
proto | RunQueryRequest the serialized RunQueryRequest |
Type | Description |
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)
Name | Description |
rpcContext | com.google.cloud.firestore.FirestoreRpcContext<?> |
queryOptions | com.google.cloud.firestore.Query.QueryOptions |
Methods
addSnapshotListener(EventListener<QuerySnapshot> listener)
public ListenerRegistration addSnapshotListener(EventListener<QuerySnapshot> listener)
Starts listening to this query.
Name | Description |
listener | EventListener<QuerySnapshot> The event listener that will be called with the snapshots. |
Type | Description |
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.
Name | Description |
executor | Executor The executor to use to call the listener. |
listener | EventListener<QuerySnapshot> The event listener that will be called with the snapshots. |
Type | Description |
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).
Type | Description |
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.
Name | Description |
snapshot | DocumentSnapshot The snapshot of the document to end at. |
Type | Description |
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.
Name | Description |
fieldValues | Object[] The field values to end this query at, in order of the query's order by. |
Type | Description |
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.
Name | Description |
snapshot | DocumentSnapshot The snapshot of the document to end before. |
Type | Description |
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.
Name | Description |
fieldValues | Object[] The field values to end this query before, in order of the query's order by. |
Type | Description |
Query | The created Query. |
equals(Object obj)
public boolean equals(Object obj)
Returns true if this Query is equal to the provided object.
Name | Description |
obj | Object The object to compare against. |
Type | Description |
boolean | Whether this Query is equal to the provided object. |
get()
public ApiFuture<QuerySnapshot> get()
Executes the query and returns the results as QuerySnapshot.
Type | Description |
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.
Type | Description |
Firestore | The Firestore instance associated with this query. |
hashCode()
public int hashCode()
Type | Description |
int |
limit(int limit)
public Query limit(int limit)
Creates and returns a new Query that only returns the first matching documents.
Name | Description |
limit | int The maximum number of items to return. |
Type | Description |
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.
Name | Description |
limit | int the maximum number of items to return |
Type | Description |
Query | the created Query |
offset(int offset)
public Query offset(int offset)
Creates and returns a new Query that skips the first n results.
Name | Description |
offset | int The number of items to skip. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The field to sort by. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The field to sort by. |
direction | Query.Direction The direction to sort. |
Type | Description |
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.
Name | Description |
field | String The field to sort by. |
Type | Description |
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.
Name | Description |
field | String The field to sort by. |
direction | Query.Direction The direction to sort. |
Type | Description |
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.
Name | Description |
fieldPaths | FieldPath[] The field paths to include. |
Type | Description |
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.
Name | Description |
fields | String[] The fields to include. |
Type | Description |
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.
Name | Description |
snapshot | DocumentSnapshot The snapshot of the document to start after. |
Type | Description |
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.
Name | Description |
fieldValues | Object[] The field values to start this query after, in order of the query's order by. |
Type | Description |
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.
Name | Description |
snapshot | DocumentSnapshot The snapshot of the document to start at. |
Type | Description |
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.
Name | Description |
fieldValues | Object[] The field values to start this query at, in order of the query's order by. |
Type | Description |
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.
Name | Description |
responseObserver | ApiStreamObserver<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.
Type | Description |
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().
Name | Description |
fieldPath | FieldPath The path of the field containing an array to search |
value | Object The value that must be contained in the array |
Type | Description |
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().
Name | Description |
field | String The name of the field containing an array to search |
value | Object The value that must be contained in the array |
Type | Description |
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().
Name | Description |
fieldPath | FieldPath The path of the field containing an array to search. |
values | List<? extends java.lang.Object> A list that contains the values to match. |
Type | Description |
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().
Name | Description |
field | String The name of the field containing an array to search. |
values | List<? extends java.lang.Object> A list that contains the values to match. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The path of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
field | String The name of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The path of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
field | String The name of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The path of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
field | String The name of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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().
Name | Description |
fieldPath | FieldPath The path of the field to search. |
values | List<? extends java.lang.Object> A list that contains the values to match. |
Type | Description |
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().
Name | Description |
field | String The name of the field to search. |
values | List<? extends java.lang.Object> A list that contains the values to match. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The path of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
field | String The name of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The path of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
field | String The name of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
fieldPath | FieldPath The path of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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.
Name | Description |
field | String The name of the field to compare. |
value | Object The value for comparison. |
Type | Description |
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().
Name | Description |
fieldPath | FieldPath The path of the field to search. |
values | List<? extends java.lang.Object> The list that contains the values to match. |
Type | Description |
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().
Name | Description |
field | String The name of the field to search. |
values | List<? extends java.lang.Object> The list that contains the values to match. |
Type | Description |
Query | The created Query. |