Class Query
- Direct Known Subclasses:
CollectionGroup
,CollectionReference
-
Nested Class Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
Query
(com.google.cloud.firestore.FirestoreRpcContext<?> rpcContext, com.google.cloud.firestore.Query.QueryOptions queryOptions) -
Method Summary
Modifier and TypeMethodDescriptionaddSnapshotListener
(EventListener<QuerySnapshot> listener) Starts listening to this query.addSnapshotListener
(Executor executor, EventListener<QuerySnapshot> listener) Starts listening to this query.count()
Returns a query that counts the documents in the result set of this query.endAt
(DocumentSnapshot snapshot) Creates and returns a new Query that ends at the provided document (inclusive).Creates and returns a new Query that ends at the provided fields relative to the order of the query.endBefore
(DocumentSnapshot snapshot) Creates and returns a new Query that ends before the provided document (exclusive).Creates and returns a new Query that ends before the provided fields relative to the order of the query.boolean
Returns true if this Query is equal to the provided object.static Query
fromProto
(Firestore firestore, RunQueryRequest proto) Returns a Query instance that can be used to execute the providedRunQueryRequest
.com.google.api.core.ApiFuture<QuerySnapshot>
get()
Executes the query and returns the results as QuerySnapshot.Gets the Firestore instance associated with this query.int
hashCode()
limit
(int limit) Creates and returns a new Query that only returns the first matching documents.limitToLast
(int limit) Creates and returns a new Query that only returns the last matching documents.offset
(int offset) Creates and returns a new Query that skips the first n results.Creates and returns a new Query that's additionally sorted by the specified field.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.Creates and returns a new Query that's additionally sorted by the specified field.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.Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields.Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields.startAfter
(DocumentSnapshot snapshot) Creates and returns a new Query that starts after the provided document (exclusive).startAfter
(Object... fieldValues) Creates and returns a new Query that starts after the provided fields relative to the order of the query.startAt
(DocumentSnapshot snapshot) Creates and returns a new Query that starts at the provided document (inclusive).Creates and returns a new Query that starts at the provided fields relative to the order of the query.void
stream
(com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> responseObserver) Executes the query and streams the results as a StreamObserver of DocumentSnapshots.toProto()
Returns theRunQueryRequest
that this Query instance represents.Creates and returns a new Query with the additional filter.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.
-
Constructor Details
-
Query
protected Query(com.google.cloud.firestore.FirestoreRpcContext<?> rpcContext, com.google.cloud.firestore.Query.QueryOptions queryOptions)
-
-
Method Details
-
getFirestore
Gets the Firestore instance associated with this query.- Returns:
- The Firestore instance associated with this query.
-
whereEqualTo
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:
field
- The name of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereEqualTo
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:
fieldPath
- The path of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereNotEqualTo
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:
field
- The name of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereNotEqualTo
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:
fieldPath
- The path of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereLessThan
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:
field
- The name of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereLessThan
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:
fieldPath
- The path of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereLessThanOrEqualTo
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:
field
- The name of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereLessThanOrEqualTo
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:
fieldPath
- The path of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThan
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:
field
- The name of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThan
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:
fieldPath
- The path of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThanOrEqualTo
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:
field
- The name of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThanOrEqualTo
@Nonnull public Query whereGreaterThanOrEqualTo(@Nonnull FieldPath fieldPath, @Nonnull 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:
fieldPath
- The path of the field to compare.value
- The value for comparison.- Returns:
- The created Query.
-
whereArrayContains
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:
field
- The name of the field containing an array to searchvalue
- The value that must be contained in the array- Returns:
- The created Query.
-
whereArrayContains
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:
fieldPath
- The path of the field containing an array to searchvalue
- The value that must be contained in the array- Returns:
- The created Query.
-
whereArrayContainsAny
@Nonnull public Query whereArrayContainsAny(@Nonnull String field, @Nonnull 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:
field
- The name of the field containing an array to search.values
- A list that contains the values to match.- Returns:
- The created Query.
-
whereArrayContainsAny
@Nonnull public Query whereArrayContainsAny(@Nonnull FieldPath fieldPath, @Nonnull 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:
fieldPath
- The path of the field containing an array to search.values
- A list that contains the values to match.- Returns:
- The created Query.
-
whereIn
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:
field
- The name of the field to search.values
- A list that contains the values to match.- Returns:
- The created Query.
-
whereIn
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:
fieldPath
- The path of the field to search.values
- A list that contains the values to match.- Returns:
- The created Query.
-
whereNotIn
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:
field
- The name of the field to search.values
- The list that contains the values to match.- Returns:
- The created Query.
-
whereNotIn
@Nonnull public Query whereNotIn(@Nonnull FieldPath fieldPath, @Nonnull 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:
fieldPath
- The path of the field to search.values
- The list that contains the values to match.- Returns:
- The created Query.
-
where
Creates and returns a new Query with the additional filter.- Parameters:
filter
- The new filter to apply to the existing query.- Returns:
- The newly created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field.- Parameters:
field
- The field to sort by.- Returns:
- The created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field.- Parameters:
fieldPath
- The field to sort by.- Returns:
- The created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.- Parameters:
field
- The field to sort by.direction
- The direction to sort.- Returns:
- The created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.- Parameters:
fieldPath
- The field to sort by.direction
- The direction to sort.- Returns:
- The created Query.
-
limit
Creates and returns a new Query that only returns the first matching documents.- Parameters:
limit
- The maximum number of items to return.- Returns:
- The created Query.
-
limitToLast
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
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.- Parameters:
limit
- the maximum number of items to return- Returns:
- the created Query
-
offset
Creates and returns a new Query that skips the first n results.- Parameters:
offset
- The number of items to skip.- Returns:
- The created Query.
-
startAt
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.- Parameters:
snapshot
- The snapshot of the document to start at.- Returns:
- The created Query.
-
startAt
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.- Parameters:
fieldValues
- The field values to start this query at, in order of the query's order by.- Returns:
- The created Query.
-
select
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.- Parameters:
fields
- The fields to include.- Returns:
- The created Query.
-
select
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.- Parameters:
fieldPaths
- The field paths to include.- Returns:
- The created Query.
-
startAfter
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.- Parameters:
snapshot
- The snapshot of the document to start after.- Returns:
- The created Query.
-
startAfter
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.- Parameters:
fieldValues
- The field values to start this query after, in order of the query's order by.- Returns:
- The created Query.
-
endBefore
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.- Parameters:
snapshot
- The snapshot of the document to end before.- Returns:
- The created Query.
-
endBefore
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.- Parameters:
fieldValues
- The field values to end this query before, in order of the query's order by.- Returns:
- The created Query.
-
endAt
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.- Parameters:
fieldValues
- The field values to end this query at, in order of the query's order by.- Returns:
- The created Query.
-
endAt
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.- Parameters:
snapshot
- The snapshot of the document to end at.- Returns:
- The created Query.
-
stream
public void stream(@Nonnull com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> responseObserver) Executes the query and streams the results as a StreamObserver of DocumentSnapshots.- Parameters:
responseObserver
- The observer to be notified when results arrive.
-
toProto
Returns theRunQueryRequest
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:
- the serialized RunQueryRequest
-
fromProto
Returns a Query instance that can be used to execute the providedRunQueryRequest
.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:
firestore
- a Firestore instance to apply the query toproto
- the serialized RunQueryRequest- Returns:
- a Query instance that can be used to execute the RunQueryRequest
-
get
Executes the query and returns the results as QuerySnapshot.- Returns:
- An ApiFuture that will be resolved with the results of the Query.
-
addSnapshotListener
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull EventListener<QuerySnapshot> listener) Starts listening to this query.- Parameters:
listener
- The event listener that will be called with the snapshots.- Returns:
- A registration object that can be used to remove the listener.
-
addSnapshotListener
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull Executor executor, @Nonnull EventListener<QuerySnapshot> listener) Starts listening to this query.- Parameters:
executor
- The executor to use to call the listener.listener
- The event listener that will be called with the snapshots.- Returns:
- A registration object that can be used to remove the listener.
-
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:
- a query that counts the documents in the result set of this query.
-
equals
Returns true if this Query is equal to the provided object. -
hashCode
public int hashCode()
-