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

Protected constructor

Properties

firestore

firestore:Firestore

TheFirestore for the Firestore database (useful for performingtransactions, etc.).

Methods

endAt

  • endAt(snapshotDocumentSnapshot<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

    ReturnsQuery<T>

    The created Query.

  • endAt(...fieldValuesany[]):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

  • endBefore(snapshotDocumentSnapshot<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

    ReturnsQuery<T>

    The created Query.

  • endBefore(...fieldValuesany[]):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 aQuerySnapshot.

    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 theGetOptions parameter.

    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

  • isEqual(otherQuery<T>):boolean
  • Returns true if thisQuery is equal to the provided one.

    Parameters

    • other:Query<T>

      TheQuery to compare against.

    Returnsboolean

    true if thisQuery is equal to the provided one.

limit

  • limit(limitnumber):Query<T>
  • Creates and returns a new Query that only returns the first matchingdocuments.

    Parameters

    • limit:number

      The maximum number of items to return.

    ReturnsQuery<T>

    The created Query.

limitToLast

  • limitToLast(limitnumber):Query<T>
  • Creates and returns a new Query that only returns the last matchingdocuments.

    You must specify at least oneorderBy clause forlimitToLast queries,otherwise an exception will be thrown during execution.

    Parameters

    • limit:number

      The maximum number of items to return.

    ReturnsQuery<T>

    The created Query.

onSnapshot

  • onSnapshot(observer{complete?:() =>void;error?:(errorFirestoreError) =>void;next?:(snapshotQuerySnapshot<T>) =>void }):() =>void
  • Attaches a listener for QuerySnapshot events. You may either passindividualonNext andonError callbacks or pass a single observerobject withnext anderror callbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshot is called.

    NOTE: Although anonCompletion callback can be provided, it willnever be called because the snapshot stream is never-ending.

    Parameters

    Returns() =>void

    An unsubscribe function that can be called to cancelthe snapshot listener.

      • ():void
      • Returnsvoid

  • onSnapshot(optionsSnapshotListenOptions, observer{complete?:() =>void;error?:(errorFirestoreError) =>void;next?:(snapshotQuerySnapshot<T>) =>void }):() =>void
  • Attaches a listener for QuerySnapshot events. You may either passindividualonNext andonError callbacks or pass a single observerobject withnext anderror callbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshot is called.

    NOTE: Although anonCompletion callback can be provided, it willnever be called because the snapshot stream is never-ending.

    Parameters

    Returns() =>void

    An unsubscribe function that can be called to cancelthe snapshot listener.

      • ():void
      • Returnsvoid

  • onSnapshot(onNext(snapshotQuerySnapshot<T>) =>void, onError?: (errorFirestoreError) =>void, onCompletion?: () =>void):() =>void
  • Attaches a listener for QuerySnapshot events. You may either passindividualonNext andonError callbacks or pass a single observerobject withnext anderror callbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshot is called.

    NOTE: Although anonCompletion callback 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 newQuerySnapshotis available.

    • Optional onError:(error:FirestoreError) =>void

      A callback to be called if the listen fails or iscancelled. No further callbacks will occur.

    • Optional onCompletion:() =>void
        • ():void
        • Returnsvoid

    Returns() =>void

    An unsubscribe function that can be called to cancelthe snapshot listener.

      • ():void
      • Returnsvoid

  • onSnapshot(optionsSnapshotListenOptions, onNext(snapshotQuerySnapshot<T>) =>void, onError?: (errorFirestoreError) =>void, onCompletion?: () =>void):() =>void
  • Attaches a listener for QuerySnapshot events. You may either passindividualonNext andonError callbacks or pass a single observerobject withnext anderror callbacks. The listener can be cancelled bycalling the function that is returned whenonSnapshot is called.

    NOTE: Although anonCompletion callback can be provided, it willnever be called because the snapshot stream is never-ending.

    Parameters

    Returns() =>void

    An unsubscribe function that can be called to cancelthe snapshot listener.

      • ():void
      • Returnsvoid

orderBy

  • orderBy(fieldPathstring |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 (asc ordesc). Ifnot specified, order will be ascending.

    ReturnsQuery<T>

    The created Query.

startAfter

  • startAfter(snapshotDocumentSnapshot<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

    ReturnsQuery<T>

    The created Query.

  • startAfter(...fieldValuesany[]):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

  • startAt(snapshotDocumentSnapshot<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 theorderBy ofthis query.

    Parameters

    ReturnsQuery<T>

    The created Query.

  • startAt(...fieldValuesany[]):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(fieldPathstring |FieldPath, opStrWhereFilterOp, valueany):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

  • withConverter(converternull):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 innull as the converter parameter removes the currentconverter.

    Parameters

    • converter:null

      Converts objects to and from Firestore. Passing innull removes the current converter.

    ReturnsQuery<DocumentData>

    A Query that uses the provided converter.

  • withConverter<U>(converterFirestoreDataConverter<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 innull as the converter parameter removes the currentconverter.

    Type parameters

    • U

    Parameters

    • converter:FirestoreDataConverter<U>

      Converts objects to and from Firestore. Passing innull removes 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.