Query
class Query
CollectionReference |
A |
A Query which you can read or listen to. You can also construct refined Query objects by adding filters and ordering.
Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.
Summary
Nested types |
|---|
enum Query.DirectionAn enum for the direction of a sort. |
Public functions |
|
|---|---|
ListenerRegistration |
addSnapshotListener(listener: EventListener<QuerySnapshot!>)Starts listening to this query. |
ListenerRegistration |
addSnapshotListener(Starts listening to this query using an Activity-scoped listener. |
ListenerRegistration |
addSnapshotListener(Starts listening to this query. |
ListenerRegistration |
addSnapshotListener(Starts listening to this query with the given options. |
ListenerRegistration |
addSnapshotListener(Starts listening to this query with the given options. |
ListenerRegistration |
addSnapshotListener(Starts listening to this query with the given options, using an Activity-scoped listener. |
ListenerRegistration |
addSnapshotListener(Starts listening to this query with the given options. |
AggregateQuery |
aggregate(Calculates the specified aggregations over the documents in the result set of the given query without actually downloading the documents. |
AggregateQuery |
count()Returns a query that counts the documents in the result set of this query. |
Query |
Creates and returns a new |
Query |
endAt(snapshot: DocumentSnapshot)Creates and returns a new |
Query |
Creates and returns a new |
Query |
endBefore(snapshot: DocumentSnapshot)Creates and returns a new |
Boolean |
|
Task<QuerySnapshot!> |
get()Executes the query and returns the results as a |
Task<QuerySnapshot!> |
Executes the query and returns the results as a |
Int |
hashCode() |
Query |
Creates and returns a new |
Query |
limitToLast(limit: Long)Creates and returns a new |
Query |
Creates and returns a new |
Query |
Creates and returns a new |
Query |
orderBy(field: String, direction: Query.Direction)Creates and returns a new |
Query |
orderBy(fieldPath: FieldPath, direction: Query.Direction)Creates and returns a new |
Query |
startAfter(fieldValues: Array<Any!>!)Creates and returns a new |
Query |
startAfter(snapshot: DocumentSnapshot)Creates and returns a new |
Query |
Creates and returns a new |
Query |
startAt(snapshot: DocumentSnapshot)Creates and returns a new |
Query |
Creates and returns a new |
Query |
whereArrayContains(field: String, value: Any)Creates and returns a new |
Query |
whereArrayContains(fieldPath: FieldPath, value: Any)Creates and returns a new |
Query |
whereArrayContainsAny(field: String, values: (Mutable)List<Any!>)Creates and returns a new |
Query |
whereArrayContainsAny(fieldPath: FieldPath, values: (Mutable)List<Any!>)Creates and returns a new |
Query |
whereEqualTo(field: String, value: Any?)Creates and returns a new |
Query |
whereEqualTo(fieldPath: FieldPath, value: Any?)Creates and returns a new |
Query |
whereGreaterThan(field: String, value: Any)Creates and returns a new |
Query |
whereGreaterThan(fieldPath: FieldPath, value: Any)Creates and returns a new |
Query |
whereGreaterThanOrEqualTo(field: String, value: Any)Creates and returns a new |
Query |
whereGreaterThanOrEqualTo(fieldPath: FieldPath, value: Any)Creates and returns a new |
Query |
Creates and returns a new |
Query |
Creates and returns a new |
Query |
whereLessThan(field: String, value: Any)Creates and returns a new |
Query |
whereLessThan(fieldPath: FieldPath, value: Any)Creates and returns a new |
Query |
whereLessThanOrEqualTo(field: String, value: Any)Creates and returns a new |
Query |
whereLessThanOrEqualTo(fieldPath: FieldPath, value: Any)Creates and returns a new |
Query |
whereNotEqualTo(field: String, value: Any?)Creates and returns a new |
Query |
whereNotEqualTo(fieldPath: FieldPath, value: Any?)Creates and returns a new |
Query |
whereNotIn(field: String, values: (Mutable)List<Any!>)Creates and returns a new |
Query |
whereNotIn(fieldPath: FieldPath, values: (Mutable)List<Any!>)Creates and returns a new |
Public properties |
|
|---|---|
FirebaseFirestore! |
Extension functions |
|
|---|---|
inline Flow<List<T>> |
<T : Any> Query.dataObjects(metadataChanges: MetadataChanges)Starts listening to this query with the given options and emits its values converted to a POJO via a |
Flow<QuerySnapshot> |
Query.snapshots(metadataChanges: MetadataChanges)Starts listening to this query with the given options and emits its values via a |
Public functions
addSnapshotListener
fun addSnapshotListener(listener: EventListener<QuerySnapshot!>): ListenerRegistration
Starts listening to this query.
| Parameters | |
|---|---|
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
fun addSnapshotListener(
activity: Activity,
listener: EventListener<QuerySnapshot!>
): ListenerRegistration
Starts listening to this query using an Activity-scoped listener.
The listener will be automatically removed during onStop.
| Parameters | |
|---|---|
activity: Activity |
The activity to scope the listener to. |
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
fun addSnapshotListener(
executor: Executor,
listener: EventListener<QuerySnapshot!>
): ListenerRegistration
Starts listening to this query.
| Parameters | |
|---|---|
executor: Executor |
The executor to use to call the listener. |
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
fun addSnapshotListener(
metadataChanges: MetadataChanges,
listener: EventListener<QuerySnapshot!>
): ListenerRegistration
Starts listening to this query with the given options.
| Parameters | |
|---|---|
metadataChanges: MetadataChanges |
Indicates whether metadata-only changes (specifically, only |
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
fun addSnapshotListener(
options: SnapshotListenOptions,
listener: EventListener<QuerySnapshot!>
): ListenerRegistration
Starts listening to this query with the given options.
| Parameters | |
|---|---|
options: SnapshotListenOptions |
Sets snapshot listener options, including whether metadata-only changes should trigger snapshot events, the source to listen to, the executor to use to call the listener, or the activity to scope the listener to. |
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
fun addSnapshotListener(
activity: Activity,
metadataChanges: MetadataChanges,
listener: EventListener<QuerySnapshot!>
): ListenerRegistration
Starts listening to this query with the given options, using an Activity-scoped listener.
The listener will be automatically removed during onStop.
| Parameters | |
|---|---|
activity: Activity |
The activity to scope the listener to. |
metadataChanges: MetadataChanges |
Indicates whether metadata-only changes (specifically, only |
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
addSnapshotListener
fun addSnapshotListener(
executor: Executor,
metadataChanges: MetadataChanges,
listener: EventListener<QuerySnapshot!>
): ListenerRegistration
Starts listening to this query with the given options.
| Parameters | |
|---|---|
executor: Executor |
The executor to use to call the listener. |
metadataChanges: MetadataChanges |
Indicates whether metadata-only changes (specifically, only |
listener: EventListener<QuerySnapshot!> |
The event listener that will be called with the snapshots. |
| Returns | |
|---|---|
ListenerRegistration |
A registration object that can be used to remove the listener. |
aggregate
fun aggregate(
aggregateField: AggregateField,
aggregateFields: Array<AggregateField!>
): AggregateQuery
Calculates the specified aggregations over the documents in the result set of the given query without actually downloading the documents.
Using the returned query to perform aggregations is efficient because only the final aggregation values, not the documents' data, is downloaded. The returned query can perform aggregations of the documents in cases where the result set is prohibitively large to download entirely (thousands of documents).
| Returns | |
|---|---|
AggregateQuery |
The |
count
fun count(): AggregateQuery
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 count the documents in cases where the result set is prohibitively large to download entirely (thousands of documents).
| Returns | |
|---|---|
AggregateQuery |
The |
endAt
fun endAt(fieldValues: Array<Any!>!): Query
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: Array<Any!>! |
The field values to end this query at, in order of the query's order by. |
| Returns | |
|---|---|
Query |
The created |
endAt
fun endAt(snapshot: DocumentSnapshot): Query
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: DocumentSnapshot |
The snapshot of the document to end at. |
| Returns | |
|---|---|
Query |
The created |
endBefore
fun endBefore(fieldValues: Array<Any!>!): Query
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: Array<Any!>! |
The field values to end this query before, in order of the query's order by. |
| Returns | |
|---|---|
Query |
The created |
endBefore
fun endBefore(snapshot: DocumentSnapshot): Query
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: DocumentSnapshot |
The snapshot of the document to end before. |
| Returns | |
|---|---|
Query |
The created |
get
fun get(): Task<QuerySnapshot!>
Executes the query and returns the results as a QuerySnapshot.
| Returns | |
|---|---|
Task<QuerySnapshot!> |
A Task that will be resolved with the results of the |
get
fun get(source: Source): Task<QuerySnapshot!>
Executes the query and returns the results as a QuerySnapshot.
By default, get() attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. This behavior can be altered via the Source parameter.
| Parameters | |
|---|---|
source: Source |
A value to configure the get behavior. |
| Returns | |
|---|---|
Task<QuerySnapshot!> |
A Task that will be resolved with the results of the |
limit
fun limit(limit: Long): Query
Creates and returns a new Query that only returns the first matching documents up to the specified number.
| Parameters | |
|---|---|
limit: Long |
The maximum number of items to return. |
| Returns | |
|---|---|
Query |
The created |
limitToLast
fun limitToLast(limit: Long): Query
Creates and returns a new Query that only returns the last matching documents up to the specified number.
You must specify at least one orderBy clause for limitToLast queries, otherwise an exception will be thrown during execution.
| Parameters | |
|---|---|
limit: Long |
The maximum number of items to return. |
| Returns | |
|---|---|
Query |
The created |
orderBy
fun orderBy(field: String): Query
Creates and returns a new Query that's additionally sorted by the specified field.
| Parameters | |
|---|---|
field: String |
The field to sort by. |
| Returns | |
|---|---|
Query |
The created |
orderBy
fun orderBy(fieldPath: FieldPath): Query
Creates and returns a new Query that's additionally sorted by the specified field.
| Parameters | |
|---|---|
fieldPath: FieldPath |
The field to sort by. |
| Returns | |
|---|---|
Query |
The created |
orderBy
fun orderBy(field: String, direction: Query.Direction): Query
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.
| Parameters | |
|---|---|
field: String |
The field to sort by. |
direction: Query.Direction |
The direction to sort. |
| Returns | |
|---|---|
Query |
The created |
orderBy
fun orderBy(fieldPath: FieldPath, direction: Query.Direction): Query
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.
| Parameters | |
|---|---|
fieldPath: FieldPath |
The field to sort by. |
direction: Query.Direction |
The direction to sort. |
| Returns | |
|---|---|
Query |
The created |
startAfter
fun startAfter(fieldValues: Array<Any!>!): Query
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: Array<Any!>! |
The field values to start this query after, in order of the query's order by. |
| Returns | |
|---|---|
Query |
The created |
startAfter
fun startAfter(snapshot: DocumentSnapshot): Query
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: DocumentSnapshot |
The snapshot of the document to start after. |
| Returns | |
|---|---|
Query |
The created |
startAt
fun startAt(fieldValues: Array<Any!>!): Query
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: Array<Any!>! |
The field values to start this query at, in order of the query's order by. |
| Returns | |
|---|---|
Query |
The created |
startAt
fun startAt(snapshot: DocumentSnapshot): Query
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: DocumentSnapshot |
The snapshot of the document to start at. |
| Returns | |
|---|---|
Query |
The created |
where
fun where(filter: Filter): Query
Creates and returns a new Query with the additional filter.
| Parameters | |
|---|---|
filter: Filter |
The new filter to apply to the existing query. |
| Returns | |
|---|---|
Query |
The newly created |
whereArrayContains
fun whereArrayContains(field: String, value: Any): Query
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: String |
The name of the field containing an array to search. |
value: Any |
The value that must be contained in the array |
| Returns | |
|---|---|
Query |
The created |
whereArrayContains
fun whereArrayContains(fieldPath: FieldPath, value: Any): Query
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: FieldPath |
The path of the field containing an array to search. |
value: Any |
The value that must be contained in the array |
| Returns | |
|---|---|
Query |
The created |
whereArrayContainsAny
fun whereArrayContainsAny(field: String, values: (Mutable)List<Any!>): Query
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: String |
The name of the field containing an array to search. |
values: (Mutable)List<Any!> |
The list that contains the values to match. |
| Returns | |
|---|---|
Query |
The created |
whereArrayContainsAny
fun whereArrayContainsAny(fieldPath: FieldPath, values: (Mutable)List<Any!>): Query
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: FieldPath |
The path of the field containing an array to search. |
values: (Mutable)List<Any!> |
The list that contains the values to match. |
| Returns | |
|---|---|
Query |
The created |
whereEqualTo
fun whereEqualTo(field: String, value: Any?): Query
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.
| Returns | |
|---|---|
Query |
The created |
whereEqualTo
fun whereEqualTo(fieldPath: FieldPath, value: Any?): Query
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: FieldPath |
The path of the field to compare |
value: Any? |
The value for comparison |
| Returns | |
|---|---|
Query |
The created |
whereGreaterThan
fun whereGreaterThan(field: String, value: Any): Query
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.
| Returns | |
|---|---|
Query |
The created |
whereGreaterThan
fun whereGreaterThan(fieldPath: FieldPath, value: Any): Query
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: FieldPath |
The path of the field to compare |
value: Any |
The value for comparison |
| Returns | |
|---|---|
Query |
The created |
whereGreaterThanOrEqualTo
fun whereGreaterThanOrEqualTo(field: String, value: Any): Query
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.
| Returns | |
|---|---|
Query |
The created |
whereGreaterThanOrEqualTo
fun whereGreaterThanOrEqualTo(fieldPath: FieldPath, value: Any): Query
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: FieldPath |
The path of the field to compare |
value: Any |
The value for comparison |
| Returns | |
|---|---|
Query |
The created |
whereIn
fun whereIn(field: String, values: (Mutable)List<Any!>): Query
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: String |
The name of the field to search. |
values: (Mutable)List<Any!> |
The list that contains the values to match. |
| Returns | |
|---|---|
Query |
The created |
whereIn
fun whereIn(fieldPath: FieldPath, values: (Mutable)List<Any!>): Query
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: FieldPath |
The path of the field to search. |
values: (Mutable)List<Any!> |
The list that contains the values to match. |
| Returns | |
|---|---|
Query |
The created |
whereLessThan
fun whereLessThan(field: String, value: Any): Query
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.
| Returns | |
|---|---|
Query |
The created |
whereLessThan
fun whereLessThan(fieldPath: FieldPath, value: Any): Query
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: FieldPath |
The path of the field to compare |
value: Any |
The value for comparison |
| Returns | |
|---|---|
Query |
The created |
whereLessThanOrEqualTo
fun whereLessThanOrEqualTo(field: String, value: Any): Query
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than or equal to the specified value.
| Returns | |
|---|---|
Query |
The created |
whereLessThanOrEqualTo
fun whereLessThanOrEqualTo(fieldPath: FieldPath, value: Any): Query
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than or equal to the specified value.
| Parameters | |
|---|---|
fieldPath: FieldPath |
The path of the field to compare |
value: Any |
The value for comparison |
| Returns | |
|---|---|
Query |
The created |
whereNotEqualTo
fun whereNotEqualTo(field: String, value: Any?): Query
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.
A Query can have only one whereNotEqualTo() filter, and it cannot be combined with whereNotIn().
| Returns | |
|---|---|
Query |
The created |
whereNotEqualTo
fun whereNotEqualTo(fieldPath: FieldPath, value: Any?): Query
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.
A Query can have only one whereNotEqualTo() filter, and it cannot be combined with whereNotIn().
| Parameters | |
|---|---|
fieldPath: FieldPath |
The path of the field to compare |
value: Any? |
The value for comparison |
| Returns | |
|---|---|
Query |
The created |
whereNotIn
fun whereNotIn(field: String, values: (Mutable)List<Any!>): Query
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.
One special case is that whereNotIn cannot match null values. To query for documents where a field exists and is null, use whereNotEqualTo, which can handle this special case.
A Query can have only one whereNotIn() filter, and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
| Parameters | |
|---|---|
field: String |
The name of the field to search. |
values: (Mutable)List<Any!> |
The list that contains the values to match. |
| Returns | |
|---|---|
Query |
The created |
whereNotIn
fun whereNotIn(fieldPath: FieldPath, values: (Mutable)List<Any!>): Query
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.
One special case is that whereNotIn cannot match null values. To query for documents where a field exists and is null, use whereNotEqualTo, which can handle this special case.
A Query can have only one whereNotIn() filter, and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
| Parameters | |
|---|---|
fieldPath: FieldPath |
The path of the field to search. |
values: (Mutable)List<Any!> |
The list that contains the values to match. |
| Returns | |
|---|---|
Query |
The created |
Public properties
Extension functions
dataObjects
inline fun <T : Any> Query.dataObjects(
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
): Flow<List<T>>
Starts listening to this query with the given options and emits its values converted to a POJO via a Flow.
-
When the returned flow starts being collected, an
EventListenerwill be attached. -
When the flow completes, the listener will be removed.
| Parameters | |
|---|---|
<T : Any> |
The type of the object to convert to. |
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE |
controls metadata-only changes. Default: |
snapshots
fun Query.snapshots(
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
): Flow<QuerySnapshot>
Starts listening to this query with the given options and emits its values via a Flow.
-
When the returned flow starts being collected, an
EventListenerwill be attached. -
When the flow completes, the listener will be removed.
| Parameters | |
|---|---|
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE |
controls metadata-only changes. Default: |