FirebaseFirestore Framework Reference

Query

classQuery:NSObject,@uncheckedSendable

AQuery refers to a query which you can read or listen to. You can also constructrefinedQuery objects by adding filters and ordering.

  • TheFirestore instance that created this query (useful for performing transactions, etc.).

    Declaration

    Swift

    varfirestore:FIRFirestore{get}
  • Reads the documents matching this query.

    This method 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. See thegetDocuments(source:completion:) method to change this behavior.

    Declaration

    Swift

    funcgetDocuments()asyncthrows->FIRQuerySnapshot

    Parameters

    completion

    a block to execute once the documents have been successfully read. documentSet will benil only if error isnon-nil.

  • Reads the documents matching this query.

    Declaration

    Swift

    funcgetDocuments(source:FirestoreSource)asyncthrows->FIRQuerySnapshot

    Parameters

    source

    indicates whether the results should be fetched from the cache only (Source.cache), the server only (Source.server), or to attempt the server and fall back to the cache (Source.default).

    completion

    a block to execute once the documents have been successfully read. documentSet will benil only if error isnon-nil.

  • Attaches a listener forQuerySnapshot events.

    Declaration

    Swift

    funcaddSnapshotListener(_listener:@escaping(FIRQuerySnapshot?,(anyError)?)->Void)->anyListenerRegistration

    Parameters

    listener

    The listener to attach.

    Return Value

    AListenerRegistration object that can be used to remove this listener.

  • Attaches a listener forQuerySnapshot events.

    Declaration

    Swift

    funcaddSnapshotListener(includeMetadataChanges:Bool,listener:@escaping(FIRQuerySnapshot?,(anyError)?)->Void)->anyListenerRegistration

    Parameters

    includeMetadataChanges

    Whether metadata-only changes (i.e. onlyDocumentSnapshot.metadata changed) should trigger snapshot events.

    listener

    The listener to attach.

    Return Value

    AListenerRegistration that can be used to remove this listener.

  • Attaches a listener forQuerySnapshot events.

    Declaration

    Swift

    funcaddSnapshotListener(options:SnapshotListenOptions,listener:@escaping(FIRQuerySnapshot?,(anyError)?)->Void)->anyListenerRegistration

    Parameters

    options

    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

    The listener to attach.

    Return Value

    AListenerRegistration that can be used to remove this listener.

  • Creates and returns a new Query with the additional filter.

    Declaration

    Swift

    funcwhereFilter(_filter:FIRFilter)->Query

    Parameters

    filter

    The new filter to apply to the existing query.

    Return Value

    The newly created Query.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be equal to the specified value.

    Declaration

    Swift

    funcwhereField(_field:String,isEqualTovalue:Any)->Query

    Parameters

    field

    The name of the field to compare.

    value

    The value the field must be equal to.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value does not equal the specified value.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,isNotEqualTovalue:Any)->Query

    Parameters

    path

    The path of the field to compare.

    value

    The value the field must be equal to.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value does not equal the specified value.

    Declaration

    Swift

    funcwhereField(_field:String,isNotEqualTovalue:Any)->Query

    Parameters

    field

    The name of the field to compare.

    value

    The value the field must be equal to.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be equal to the specified value.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,isEqualTovalue:Any)->Query

    Parameters

    path

    The path of the field to compare.

    value

    The value the field must be equal to.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be less than the specified value.

    Declaration

    Swift

    funcwhereField(_field:String,isLessThanvalue:Any)->Query

    Parameters

    field

    The name of the field to compare.

    value

    The value the field must be less than.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be less than the specified value.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,isLessThanvalue:Any)->Query

    Parameters

    path

    The path of the field to compare.

    value

    The value the field must be less than.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be less than or equal to the specified value.

    Declaration

    Swift

    funcwhereField(_field:String,isLessThanOrEqualTovalue:Any)->Query

    Parameters

    field

    The name of the field to compare

    value

    The value the field must be less than or equal to.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be less than or equal to the specified value.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,isLessThanOrEqualTovalue:Any)->Query

    Parameters

    path

    The path of the field to compare

    value

    The value the field must be less than or equal to.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must greater than the specified value.

    Declaration

    Swift

    funcwhereField(_field:String,isGreaterThanvalue:Any)->Query

    Parameters

    field

    The name of the field to compare

    value

    The value the field must be greater than.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must greater than the specified value.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,isGreaterThanvalue:Any)->Query

    Parameters

    path

    The path of the field to compare

    value

    The value the field must be greater than.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be greater than or equal to the specified value.

    Declaration

    Swift

    funcwhereField(_field:String,isGreaterThanOrEqualTovalue:Any)->Query

    Parameters

    field

    The name of the field to compare

    value

    The value the field must be greater than.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must be greater than or equal to the specified value.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,isGreaterThanOrEqualTovalue:Any)->Query

    Parameters

    path

    The path of the field to compare

    value

    The value the field must be greater than.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field, it must be an array, and the array must contain the provided value.

    A query can have only onearrayContains filter.

    Declaration

    Swift

    funcwhereField(_field:String,arrayContainsvalue:Any)->Query

    Parameters

    field

    The name of the field containing an array to search

    value

    The value that must be contained in the array

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field, it must be an array, and the array must contain the provided value.

    A query can have only onearrayContains filter.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,arrayContainsvalue:Any)->Query

    Parameters

    path

    The path of the field containing an array to search

    value

    The value that must be contained in the array

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field, the value must be an array, and that array must contain at least one value from the provided array.

    A query can have only onearrayContainsAny filter and it cannot be combined witharrayContains orin filters.

    Declaration

    Swift

    funcwhereField(_field:String,arrayContainsAnyvalues:[Any])->Query

    Parameters

    field

    The name of the field containing an array to search.

    values

    The array that contains the values to match.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field, the value must be an array, and that array must contain at least one value from the provided array.

    A query can have only onearrayContainsAny filter and it cannot be combined witharrayContains orin filters.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,arrayContainsAnyvalues:[Any])->Query

    Parameters

    path

    The path of the field containing an array to search.

    values

    The array that contains the values to match.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided array.

    A query can have only onein filter, and it cannot be combined with anarrayContainsAny filter.

    Declaration

    Swift

    funcwhereField(_field:String,invalues:[Any])->Query

    Parameters

    field

    The name of the field to search.

    values

    The array that contains the values to match.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided array.

    A query can have only onein filter, and it cannot be combined with anarrayContainsAny filter.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,invalues:[Any])->Query

    Parameters

    path

    The path of the field to search.

    values

    The array that contains the values to match.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided array.

    One special case is thatnotIn filters cannot matchnil values. To query for documents where a field exists and isnil, use anotEqual filter, which can handle this special case.

    A query can have only onenotIn filter, and it cannot be combined with anarrayContains,arrayContainsAny,in, ornotEqual filter.

    Declaration

    Swift

    funcwhereField(_field:String,notInvalues:[Any])->Query

    Parameters

    field

    The name of the field to search.

    values

    The array that contains the values to match.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided array.

    One special case is thatnotIn filters cannot matchnil values. To query for documents where a field exists and isnil, use anotEqual filter, which can handle this special case.

    Passing in anull value into thevalues array results in no document matches. To query for documents where a field is notnull, use anotEqual filter.

    Declaration

    Swift

    funcwhereField(_path:FIRFieldPath,notInvalues:[Any])->Query

    Parameters

    path

    The path of the field to search.

    values

    The array that contains the values to match.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery with the additional filter that documents must satisfy the specified predicate.

    Declaration

    Swift

    funcfilter(usingpredicate:NSPredicate)->Query

    Parameters

    predicate

    The predicate the document must satisfy. Can be either comparison or compound of comparison. In particular, block-based predicate is not supported.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that’s additionally sorted by the specified field.

    Declaration

    Swift

    funcorder(byfield:String)->Query

    Parameters

    field

    The field to sort by.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that’s additionally sorted by the specified field.

    Declaration

    Swift

    funcorder(bypath:FIRFieldPath)->Query

    Parameters

    path

    The field to sort by.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that’s additionally sorted by the specified field, optionally in descending order instead of ascending.

    Declaration

    Swift

    funcorder(byfield:String,descending:Bool)->Query

    Parameters

    field

    The field to sort by.

    descending

    Whether to sort descending.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that’s additionally sorted by the specified field, optionally in descending order instead of ascending.

    Declaration

    Swift

    funcorder(bypath:FIRFieldPath,descending:Bool)->Query

    Parameters

    path

    The field to sort by.

    descending

    Whether to sort descending.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that only returns the first matching documents up to the specified number.

    Declaration

    Swift

    funclimit(tolimit:Int)->Query

    Parameters

    limit

    The maximum number of items to return.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that only returns the last matching documents up to the specified number.

    A query with alimit(toLast:) clause must have at least oneorderBy clause.

    Declaration

    Swift

    funclimit(toLastlimit:Int)->Query

    Parameters

    limit

    The maximum number of items to return.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcstart(atDocumentdocument:FIRDocumentSnapshot)->Query

    Parameters

    document

    The snapshot of the document to start at.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcstart(atfieldValues:[Any])->Query

    Parameters

    fieldValues

    The field values to start this query at, in order of the query’s order by.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcstart(afterDocumentdocument:FIRDocumentSnapshot)->Query

    Parameters

    document

    The snapshot of the document to start after.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcstart(afterfieldValues:[Any])->Query

    Parameters

    fieldValues

    The field values to start this query after, in order of the query’s orderBy.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcend(beforeDocumentdocument:FIRDocumentSnapshot)->Query

    Parameters

    document

    The snapshot of the document to end before.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcend(beforefieldValues:[Any])->Query

    Parameters

    fieldValues

    The field values to end this query before, in order of the query’s order by.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery that ends at 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.

    Declaration

    Swift

    funcend(atDocumentdocument:FIRDocumentSnapshot)->Query

    Parameters

    document

    The snapshot of the document to end at.

    Return Value

    The createdQuery.

  • Creates and returns a newQuery 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.

    Declaration

    Swift

    funcend(atfieldValues:[Any])->Query

    Parameters

    fieldValues

    The field values to end this query at, in order of the query’s order by.

    Return Value

    The createdQuery.

  • A query that counts the documents in the result set of this query without actually downloading the documents.

    Using thisAggregateQuery to count the documents is efficient because only the final count, not the documents’ data, is downloaded. TheAggregateQuery can count the documents in cases where the result set is prohibitively large to download entirely (thousands of documents).

    Declaration

    Swift

    varcount:FIRAggregateQuery{get}
  • Creates and returns a newAggregateQuery that aggregates the documents in the result set of this query without actually downloading the documents.

    Using anAggregateQuery to perform aggregations is efficient because only the final aggregation values, not the documents’ data, is downloaded. The returnedAggregateQuery can perform aggregations of the documents in cases where the result set is prohibitively large to download entirely (thousands of documents).

    Declaration

    Swift

    funcaggregate(_aggregateFields:[FIRAggregateField])->FIRAggregateQuery

    Parameters

    aggregateFields

    Specifies the aggregate operations to perform on the result set of thisquery.

    Return Value

    AnAggregateQuery encapsulating thisQuery andAggregateFields, which can be usedto query the server for the aggregation results.

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 2025-03-11 UTC.