@InternalExtensionOnly public class Query extends Object
Modifier and Type | Class and Description |
---|---|
static class |
Query.Direction
The direction of a sort.
|
Modifier | Constructor and Description |
---|---|
protected |
Query(com.google.cloud.firestore.FirestoreRpcContext<?> rpcContext,
com.google.cloud.firestore.Query.QueryOptions queryOptions) |
Modifier and Type | Method and Description |
---|---|
ListenerRegistration |
addSnapshotListener(EventListener<QuerySnapshot> listener)
Starts listening to this query.
|
ListenerRegistration |
addSnapshotListener(Executor executor,
EventListener<QuerySnapshot> listener)
Starts listening to this query.
|
AggregateQuery |
count()
Returns a query that counts the documents in the result set of this query.
|
Query |
endAt(DocumentSnapshot snapshot)
Creates and returns a new Query that ends at the provided document (inclusive).
|
Query |
endAt(Object... fieldValues)
Creates and returns a new Query that ends at the provided fields relative to the order of the
query.
|
Query |
endBefore(DocumentSnapshot snapshot)
Creates and returns a new Query that ends before the provided document (exclusive).
|
Query |
endBefore(Object... fieldValues)
Creates and returns a new Query that ends before the provided fields relative to the order of
the query.
|
boolean |
equals(Object obj)
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 provided
RunQueryRequest . |
com.google.api.core.ApiFuture<QuerySnapshot> |
get()
Executes the query and returns the results as QuerySnapshot.
|
Firestore |
getFirestore()
Gets the Firestore instance associated with this query.
|
int |
hashCode() |
Query |
limit(int limit)
Creates and returns a new Query that only returns the first matching documents.
|
Query |
limitToLast(int limit)
Creates and returns a new Query that only returns the last matching documents.
|
Query |
offset(int offset)
Creates and returns a new Query that skips the first n results.
|
Query |
orderBy(FieldPath fieldPath)
Creates and returns a new Query that's additionally sorted by the specified field.
|
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.
|
Query |
orderBy(String field)
Creates and returns a new Query that's additionally sorted by the specified field.
|
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.
|
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.
|
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.
|
Query |
startAfter(DocumentSnapshot snapshot)
Creates and returns a new Query that starts after the provided document (exclusive).
|
Query |
startAfter(Object... fieldValues)
Creates and returns a new Query that starts after the provided fields relative to the order of
the query.
|
Query |
startAt(DocumentSnapshot snapshot)
Creates and returns a new Query that starts at the provided document (inclusive).
|
Query |
startAt(Object... fieldValues)
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.
|
RunQueryRequest |
toProto()
Returns the
RunQueryRequest that this Query instance represents. |
Query |
where(Filter filter)
Creates and returns a new Query with the additional filter.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
protected Query(com.google.cloud.firestore.FirestoreRpcContext<?> rpcContext, com.google.cloud.firestore.Query.QueryOptions queryOptions)
@Nonnull public Firestore getFirestore()
@Nonnull public Query whereEqualTo(@Nonnull String field, @Nullable Object value)
field
- The name of the field to compare.value
- The value for comparison.@Nonnull public Query whereEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value)
fieldPath
- The path of the field to compare.value
- The value for comparison.@Nonnull public Query whereNotEqualTo(@Nonnull String field, @Nullable Object value)
field
- The name of the field to compare.value
- The value for comparison.@Nonnull public Query whereNotEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value)
fieldPath
- The path of the field to compare.value
- The value for comparison.@Nonnull public Query whereLessThan(@Nonnull String field, @Nonnull Object value)
field
- The name of the field to compare.value
- The value for comparison.@Nonnull public Query whereLessThan(@Nonnull FieldPath fieldPath, @Nonnull Object value)
fieldPath
- The path of the field to compare.value
- The value for comparison.@Nonnull public Query whereLessThanOrEqualTo(@Nonnull String field, @Nonnull Object value)
field
- The name of the field to compare.value
- The value for comparison.@Nonnull public Query whereLessThanOrEqualTo(@Nonnull FieldPath fieldPath, @Nonnull Object value)
fieldPath
- The path of the field to compare.value
- The value for comparison.@Nonnull public Query whereGreaterThan(@Nonnull String field, @Nonnull Object value)
field
- The name of the field to compare.value
- The value for comparison.@Nonnull public Query whereGreaterThan(@Nonnull FieldPath fieldPath, @Nonnull Object value)
fieldPath
- The path of the field to compare.value
- The value for comparison.@Nonnull public Query whereGreaterThanOrEqualTo(@Nonnull String field, @Nonnull Object value)
field
- The name of the field to compare.value
- The value for comparison.@Nonnull public Query whereGreaterThanOrEqualTo(@Nonnull FieldPath fieldPath, @Nonnull Object value)
fieldPath
- The path of the field to compare.value
- The value for comparison.@Nonnull public Query whereArrayContains(@Nonnull String field, @Nonnull Object value)
A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().
field
- The name of the field containing an array to searchvalue
- The value that must be contained in the array@Nonnull public Query whereArrayContains(@Nonnull FieldPath fieldPath, @Nonnull Object value)
A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().
fieldPath
- The path of the field containing an array to searchvalue
- The value that must be contained in the array@Nonnull public Query whereArrayContainsAny(@Nonnull String field, @Nonnull List<? extends Object> values)
A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().
field
- The name of the field containing an array to search.values
- A list that contains the values to match.@Nonnull public Query whereArrayContainsAny(@Nonnull FieldPath fieldPath, @Nonnull List<? extends Object> values)
A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().
fieldPath
- The path of the field containing an array to search.values
- A list that contains the values to match.@Nonnull public Query whereIn(@Nonnull String field, @Nonnull List<? extends Object> values)
A Query can have only one whereIn() filter, and it cannot be combined with whereArrayContainsAny().
field
- The name of the field to search.values
- A list that contains the values to match.@Nonnull public Query whereIn(@Nonnull FieldPath fieldPath, @Nonnull List<? extends Object> values)
A Query can have only one whereIn() filter, and it cannot be combined with whereArrayContainsAny().
fieldPath
- The path of the field to search.values
- A list that contains the values to match.@Nonnull public Query whereNotIn(@Nonnull String field, @Nonnull List<? extends Object> values)
A Query can have only one whereNotIn() filter and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
field
- The name of the field to search.values
- The list that contains the values to match.@Nonnull public Query whereNotIn(@Nonnull FieldPath fieldPath, @Nonnull List<? extends Object> values)
A Query can have only one whereNotIn() filter, and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
fieldPath
- The path of the field to search.values
- The list that contains the values to match.public Query where(Filter filter)
filter
- The new filter to apply to the existing query.@Nonnull public Query orderBy(@Nonnull String field)
field
- The field to sort by.@Nonnull public Query orderBy(@Nonnull FieldPath fieldPath)
fieldPath
- The field to sort by.@Nonnull public Query orderBy(@Nonnull String field, @Nonnull Query.Direction direction)
field
- The field to sort by.direction
- The direction to sort.@Nonnull public Query orderBy(@Nonnull FieldPath fieldPath, @Nonnull Query.Direction direction)
fieldPath
- The field to sort by.direction
- The direction to sort.@Nonnull public Query limit(int limit)
limit
- The maximum number of items to return.@Nonnull public Query limitToLast(int limit)
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.
limit
- the maximum number of items to return@Nonnull public Query offset(int offset)
offset
- The number of items to skip.@Nonnull public Query startAt(@Nonnull DocumentSnapshot snapshot)
snapshot
- The snapshot of the document to start at.@Nonnull public Query startAt(Object... fieldValues)
fieldValues
- The field values to start this query at, in order of the query's order by.@Nonnull public Query select(String... fields)
fields
- The fields to include.@Nonnull public Query select(FieldPath... fieldPaths)
fieldPaths
- The field paths to include.@Nonnull public Query startAfter(@Nonnull DocumentSnapshot snapshot)
snapshot
- The snapshot of the document to start after.public Query startAfter(Object... fieldValues)
fieldValues
- The field values to start this query after, in order of the query's order
by.@Nonnull public Query endBefore(@Nonnull DocumentSnapshot snapshot)
snapshot
- The snapshot of the document to end before.@Nonnull public Query endBefore(Object... fieldValues)
fieldValues
- The field values to end this query before, in order of the query's order by.@Nonnull public Query endAt(Object... fieldValues)
fieldValues
- The field values to end this query at, in order of the query's order by.@Nonnull public Query endAt(@Nonnull DocumentSnapshot snapshot)
snapshot
- The snapshot of the document to end at.public void stream(@Nonnull com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> responseObserver)
responseObserver
- The observer to be notified when results arrive.public RunQueryRequest toProto()
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.
public static Query fromProto(Firestore firestore, RunQueryRequest proto)
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.
firestore
- a Firestore instance to apply the query toproto
- the serialized RunQueryRequest@Nonnull public com.google.api.core.ApiFuture<QuerySnapshot> get()
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull EventListener<QuerySnapshot> listener)
listener
- The event listener that will be called with the snapshots.@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull Executor executor, @Nonnull EventListener<QuerySnapshot> listener)
executor
- The executor to use to call the listener.listener
- The event listener that will be called with the snapshots.@Nonnull public AggregateQuery count()
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).
public boolean equals(Object obj)
Copyright © 2023 Google LLC. All rights reserved.