AQuery refers to a Query which you can read or listen to. You can alsoconstruct refinedQuery objects by adding filters and ordering.
Type parameters
T
Index
Constructors
Properties
Methods
Constructors
Protected constructor
Properties
firestore
TheFirestore for the Firestore database (useful for performingtransactions, etc.).
Methods
endAt
- end
At(snapshot: DocumentSnapshot<any>):Query<T> Creates and returns a new Query that ends at the provided document(inclusive). The end position is relative to the order of the query. Thedocument must contain all of the fields provided in the orderBy of thisquery.
Parameters
snapshot:DocumentSnapshot<any>
The snapshot of the document to end at.
ReturnsQuery<T>
The created Query.
- end
At(...fieldValues: any[]):Query<T> Creates and returns a new Query that ends at the provided fieldsrelative to the order of the query. The order of the field valuesmust match the order of the order by clauses of the query.
Parameters
Rest...fieldValues:any[]
The field values to end this query at, in orderof the query's order by.
ReturnsQuery<T>
The created Query.
endBefore
- end
Before(snapshot: DocumentSnapshot<any>):Query<T> Creates and returns a new Query that ends before the provided document(exclusive). The end position is relative to the order of the query. Thedocument must contain all of the fields provided in the orderBy of thisquery.
Parameters
snapshot:DocumentSnapshot<any>
The snapshot of the document to end before.
ReturnsQuery<T>
The created Query.
- end
Before(...fieldValues: any[]):Query<T> Creates and returns a new Query that ends before the provided fieldsrelative to the order of the query. The order of the field valuesmust match the order of the order by clauses of the query.
Parameters
Rest...fieldValues:any[]
The field values to end this query before, in orderof the query's order by.
ReturnsQuery<T>
The created Query.
get
- get(options?: GetOptions):Promise<QuerySnapshot<T>>
Executes the query and returns the results as a
QuerySnapshot.Note: By default, get() attempts to provide up-to-date data when possibleby waiting for data from the server, but it may return cached data or failif you are offline and the server cannot be reached. This behavior can bealtered via the
GetOptionsparameter.Parameters
Optional options:GetOptions
An object to configure the get behavior.
ReturnsPromise<QuerySnapshot<T>>
A Promise that will be resolved with the results of the Query.
isEqual
limit
limitToLast
- limit
ToLast(limit: number):Query<T> Creates and returns a new Query that only returns the last matchingdocuments.
You must specify at least one
orderByclause forlimitToLastqueries,otherwise an exception will be thrown during execution.Parameters
limit:number
The maximum number of items to return.
ReturnsQuery<T>
The created Query.
onSnapshot
- on
Snapshot(observer: {complete?:() =>void;error?:(error: FirestoreError) =>void;next?:(snapshot: QuerySnapshot<T>) =>void }):() =>void Attaches a listener for QuerySnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshotis called.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
observer:{complete?:() =>void;error?:(error:FirestoreError) =>void;next?:(snapshot:QuerySnapshot<T>) =>void }
A single object containing
nextanderrorcallbacks.Optional complete?:() =>void
- ():void
Returnsvoid
Optional error?:(error:FirestoreError) =>void
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional next?:(snapshot:QuerySnapshot<T>) =>void
- (snapshot:QuerySnapshot<T>):void
Parameters
snapshot:QuerySnapshot<T>
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
- on
Snapshot(options: SnapshotListenOptions, observer: {complete?:() =>void;error?:(error: FirestoreError) =>void;next?:(snapshot: QuerySnapshot<T>) =>void }):() =>void Attaches a listener for QuerySnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshotis called.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
options:SnapshotListenOptions
Options controlling the listen behavior.
observer:{complete?:() =>void;error?:(error:FirestoreError) =>void;next?:(snapshot:QuerySnapshot<T>) =>void }
A single object containing
nextanderrorcallbacks.Optional complete?:() =>void
- ():void
Returnsvoid
Optional error?:(error:FirestoreError) =>void
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional next?:(snapshot:QuerySnapshot<T>) =>void
- (snapshot:QuerySnapshot<T>):void
Parameters
snapshot:QuerySnapshot<T>
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
- on
Snapshot(onNext: (snapshot: QuerySnapshot<T>) =>void, onError?: (error: FirestoreError) =>void, onCompletion?: () =>void):() =>void Attaches a listener for QuerySnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshotis called.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
onNext:(snapshot:QuerySnapshot<T>) =>void
A callback to be called every time a new
QuerySnapshotis available.- (snapshot:QuerySnapshot<T>):void
Parameters
snapshot:QuerySnapshot<T>
Returnsvoid
Optional onError:(error:FirestoreError) =>void
A callback to be called if the listen fails or iscancelled. No further callbacks will occur.
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional onCompletion:() =>void
- ():void
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
- on
Snapshot(options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot<T>) =>void, onError?: (error: FirestoreError) =>void, onCompletion?: () =>void):() =>void Attaches a listener for QuerySnapshot events. You may either passindividual
onNextandonErrorcallbacks or pass a single observerobject withnextanderrorcallbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshotis called.NOTE: Although an
onCompletioncallback can be provided, it willnever be called because the snapshot stream is never-ending.Parameters
options:SnapshotListenOptions
Options controlling the listen behavior.
onNext:(snapshot:QuerySnapshot<T>) =>void
A callback to be called every time a new
QuerySnapshotis available.- (snapshot:QuerySnapshot<T>):void
Parameters
snapshot:QuerySnapshot<T>
Returnsvoid
Optional onError:(error:FirestoreError) =>void
A callback to be called if the listen fails or iscancelled. No further callbacks will occur.
- (error:FirestoreError):void
Parameters
error:FirestoreError
Returnsvoid
Optional onCompletion:() =>void
- ():void
Returnsvoid
Returns() =>void
An unsubscribe function that can be called to cancelthe snapshot listener.
- ():void
Returnsvoid
orderBy
- order
By(fieldPath: string |FieldPath, directionStr?: OrderByDirection):Query<T> Creates and returns a new Query that's additionally sorted by thespecified field, optionally in descending order instead of ascending.
Parameters
fieldPath:string |FieldPath
The field to sort by.
Optional directionStr:OrderByDirection
Optional direction to sort by (
ascordesc). Ifnot specified, order will be ascending.
ReturnsQuery<T>
The created Query.
startAfter
- start
After(snapshot: DocumentSnapshot<any>):Query<T> 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 ofthis query.
Parameters
snapshot:DocumentSnapshot<any>
The snapshot of the document to start after.
ReturnsQuery<T>
The created Query.
- start
After(...fieldValues: any[]):Query<T> Creates and returns a new Query that starts after the provided fieldsrelative to the order of the query. The order of the field valuesmust match the order of the order by clauses of the query.
Parameters
Rest...fieldValues:any[]
The field values to start this query after, in orderof the query's order by.
ReturnsQuery<T>
The created Query.
startAt
- start
At(snapshot: DocumentSnapshot<any>):Query<T> 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
orderByofthis query.Parameters
snapshot:DocumentSnapshot<any>
The snapshot of the document to start at.
ReturnsQuery<T>
The created Query.
- start
At(...fieldValues: any[]):Query<T> Creates and returns a new Query that starts at the provided fieldsrelative to the order of the query. The order of the field valuesmust match the order of the order by clauses of the query.
Parameters
Rest...fieldValues:any[]
The field values to start this query at, in orderof the query's order by.
ReturnsQuery<T>
The created Query.
where
- where(fieldPath: string |FieldPath, opStr: WhereFilterOp, value: any):Query<T>
Creates and returns a new Query with the additional filter that documentsmust contain the specified field and the value should satisfy therelation constraint provided.
Parameters
fieldPath:string |FieldPath
The path to compare
opStr:WhereFilterOp
The operation string (e.g "<", "<=", "==", ">", ">=").
value:any
The value for comparison
ReturnsQuery<T>
The created Query.
withConverter
- with
Converter(converter: null):Query<DocumentData> Applies a custom data converter to this Query, allowing you to use yourown custom model objects with Firestore. When you call get() on thereturned Query, the provided converter will convert between Firestoredata and your custom type U.
Passing in
nullas the converter parameter removes the currentconverter.Parameters
converter:null
Converts objects to and from Firestore. Passing in
nullremoves the current converter.
ReturnsQuery<DocumentData>
A Query that uses the provided converter.
- with
Converter<U>(converter: FirestoreDataConverter<U>):Query<U> Applies a custom data converter to this Query, allowing you to use yourown custom model objects with Firestore. When you call get() on thereturned Query, the provided converter will convert between Firestoredata and your custom type U.
Passing in
nullas the converter parameter removes the currentconverter.Type parameters
U
Parameters
converter:FirestoreDataConverter<U>
Converts objects to and from Firestore. Passing in
nullremoves the current converter.
ReturnsQuery<U>
A Query that uses the provided converter.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-07-27 UTC.