Reference documentation and code samples for the Cloud Firestore Client class Query.
A Cloud Firestore Query.
This class is immutable; any filters applied will return a new instance of
the class.
Example:
use Google\Cloud\Firestore\FirestoreClient;
$firestore = new FirestoreClient();
$collection = $firestore->collection('users');
$query = $collection->where('age', '>', 18);
Namespace
Google \ Cloud \ Firestore
Methods
__construct
Parameters
Name
Description
connection
Connection\ConnectionInterface
A Connection to Cloud Firestore.
This object is created by FirestoreClient,
and should not be instantiated outside of this client.
valueMapper
ValueMapper
A Firestore Value Mapper.
parent
string
The parent of the query.
query
array
The Query object
limitToLast
bool
Limit a query to return only the last matching documents.
count
Gets the count of all documents matching the provided query filters.
Example:
$count = $query->count();
Parameters
Name
Description
options
array
Configuration options is an array.
↳ readTime
Timestamp
Reads entities as they were at the given timestamp.
Returns
Type
Description
int
sum
Gets the sum of all documents matching the provided query filters.
Example:
$sum = $query->sum();
Sum of integers which exceed maximum integer value returns a float.
Sum of numbers exceeding max float value returnsINF.
Sum of data which containsNaNreturnsNaN.
Non numeric values are ignored.
Parameters
Name
Description
field
string
The relative path of the field to aggregate upon.
options
array
Configuration options is an array.
↳ readTime
Timestamp
Reads entities as they were at the given timestamp.
Returns
Type
Description
int|float
avg
Gets the average of all documents matching the provided query filters.
Example:
$avg = $query->avg();
Average of empty valid data set returnnull.
Average of numbers exceeding max float value returnsINF.
Average of data which containsNaNreturnsNaN.
Non numeric values are ignored.
Parameters
Name
Description
field
string
The relative path of the field to aggregate upon.
options
array
Configuration options is an array.
↳ readTime
Timestamp
Reads entities as they were at the given timestamp.
Returns
Type
Description
float|null
addAggregation
Returns an aggregate query provided an aggregation with existing query filters.
Creates and returns a new Query instance that applies a field mask to the
result and returns only the specified subset of fields. You can specify a
list of field paths to return, or use an empty list to only return the
references of matching documents.
Subsequent calls to this method will override previous values.
A new instance of Query with the given changes applied.
where
Add a WHERE clause to the Query.
For a list of all available operators, seeV1\StructuredQuery\FieldFilter\Operator.
This method also supports a number of comparison operators which you will
be familiar with, such as=,>,<,<=and>=. For array fields,
thearray-contains,INandarray-contains-anyoperators are also
available.
This method also supports usage of Filters (seeFilter).
The Filter class helps to create complex queries using AND and OR operators.
Example:
$query = $query->where('firstName', '=', 'John');
// Filtering against `null` and `NAN` is supported only with the equality operator.
$query = $query->where('coolnessPercentage', '=', NAN);
// Use `array-contains` to select documents where the array contains given elements.
$query = $query->where('friends', 'array-contains', ['Steve', 'Sarah']);
use Google\Cloud\Firestore\Filter;
// Filtering with Filter::or
$query = $query->where(Filter::or([
Filter::field('firstName', '=', 'John'),
Filter::field('firstName', '=', 'Monica')
]));
A new instance of Query with the given changes applied.
startAt
A starting point for the query results.
Starts results at the provided set of field values relative to the order
of the query. The order of the provided values must match the order of
the order by clauses of the query. Values in the given cursor will be
included in the result set, if found.
Multiple invocations of this call overwrite previous calls. CallingstartAt()will overwrite both previousstartAt()andstartAfter()calls.
A new instance of Query with the given changes applied.
startAfter
A starting point for the query results.
Starts results after the provided set of field values relative to the order
of the query. The order of the provided values must match the order of
the order by clauses of the query. Values in the given cursor will not be
included in the result set.
Multiple invocations of this call overwrite previous calls. CallingstartAt()will overwrite both previousstartAt()andstartAfter()calls.
A new instance of Query with the given changes applied.
endBefore
An end point for the query results.
Ends results before the provided set of field values relative to the order
of the query. The order of the provided values must match the order of
the order by clauses of the query. Values in the given cursor will be
included in the result set, if found.
Multiple invocations of this call overwrite previous calls. CallingendBefore()will overwrite both previousendBefore()andendAt()calls.
A new instance of Query with the given changes applied.
endAt
An end point for the query results.
Ends results at the provided set of field values relative to the order
of the query. The order of the provided values must match the order of
the order by clauses of the query. Values in the given cursor will not be
included in the result set.
Multiple invocations of this call overwrite previous calls. CallingendBefore()will overwrite both previousendBefore()andendAt()calls.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Firestore Client - Class Query (1.53.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.53.0 (latest)](/php/docs/reference/cloud-firestore/latest/Query)\n- [1.52.0](/php/docs/reference/cloud-firestore/1.52.0/Query)\n- [1.51.0](/php/docs/reference/cloud-firestore/1.51.0/Query)\n- [1.50.0](/php/docs/reference/cloud-firestore/1.50.0/Query)\n- [1.48.1](/php/docs/reference/cloud-firestore/1.48.1/Query)\n- [1.47.3](/php/docs/reference/cloud-firestore/1.47.3/Query)\n- [1.46.0](/php/docs/reference/cloud-firestore/1.46.0/Query)\n- [1.45.2](/php/docs/reference/cloud-firestore/1.45.2/Query)\n- [1.44.1](/php/docs/reference/cloud-firestore/1.44.1/Query)\n- [1.43.3](/php/docs/reference/cloud-firestore/1.43.3/Query)\n- [1.40.0](/php/docs/reference/cloud-firestore/1.40.0/Query)\n- [1.39.0](/php/docs/reference/cloud-firestore/1.39.0/Query)\n- [1.38.0](/php/docs/reference/cloud-firestore/1.38.0/Query)\n- [1.37.7](/php/docs/reference/cloud-firestore/1.37.7/Query)\n- [1.36.1](/php/docs/reference/cloud-firestore/1.36.1/Query)\n- [1.35.0](/php/docs/reference/cloud-firestore/1.35.0/Query)\n- [1.34.0](/php/docs/reference/cloud-firestore/1.34.0/Query)\n- [1.33.0](/php/docs/reference/cloud-firestore/1.33.0/Query)\n- [1.32.0](/php/docs/reference/cloud-firestore/1.32.0/Query)\n- [1.31.0](/php/docs/reference/cloud-firestore/1.31.0/Query)\n- [1.30.0](/php/docs/reference/cloud-firestore/1.30.0/Query)\n- [1.28.0](/php/docs/reference/cloud-firestore/1.28.0/Query)\n- [1.27.3](/php/docs/reference/cloud-firestore/1.27.3/Query) \nReference documentation and code samples for the Cloud Firestore Client class Query.\n\nA Cloud Firestore Query.\n\nThis class is immutable; any filters applied will return a new instance of\nthe class.\n\nExample: \n\n use Google\\Cloud\\Firestore\\FirestoreClient;\n\n $firestore = new FirestoreClient();\n\n $collection = $firestore-\u003ecollection('users');\n $query = $collection-\u003ewhere('age', '\u003e', 18);\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Firestore\n\nMethods\n-------\n\n### __construct\n\n### count\n\nGets the count of all documents matching the provided query filters.\n\nExample: \n\n $count = $query-\u003ecount();\n\n### sum\n\nGets the sum of all documents matching the provided query filters.\n\nExample: \n\n $sum = $query-\u003esum();\n\nSum of integers which exceed maximum integer value returns a float.\nSum of numbers exceeding max float value returns `INF`.\nSum of data which contains `NaN` returns `NaN`.\nNon numeric values are ignored.\n\n### avg\n\nGets the average of all documents matching the provided query filters.\n\nExample: \n\n $avg = $query-\u003eavg();\n\nAverage of empty valid data set return `null`.\nAverage of numbers exceeding max float value returns `INF`.\nAverage of data which contains `NaN` returns `NaN`.\nNon numeric values are ignored.\n\n### addAggregation\n\nReturns an aggregate query provided an aggregation with existing query filters.\n\nExample: \n\n use Google\\Cloud\\Firestore\\Aggregate;\n\n $aggregation = Aggregate::count()-\u003ealias('count_upto_1');\n $aggregateQuery = $query-\u003elimit(1)-\u003eaddAggregation($aggregation);\n $aggregateQuerySnapshot = $aggregateQuery-\u003egetSnapshot();\n $countUpto1 = $aggregateQuerySnapshot-\u003eget('count_upto_1');\n\n### documents\n\nSee also:\n\n- [RunQuery](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.Firestore.RunQuery)\n\n### select\n\nAdd a SELECT to the Query.\n\nCreates and returns a new Query instance that applies a field mask to the\nresult and returns only the specified subset of fields. You can specify a\nlist of field paths to return, or use an empty list to only return the\nreferences of matching documents.\n\nSubsequent calls to this method will override previous values.\n\nExample: \n\n $query = $query-\u003eselect(['firstName']);\n\n### where\n\nAdd a WHERE clause to the Query.\n\nFor a list of all available operators, see\n[V1\\\\StructuredQuery\\\\FieldFilter\\\\Operator](/php/docs/reference/cloud-firestore/latest/V1.StructuredQuery.FieldFilter.Operator).\nThis method also supports a number of comparison operators which you will\nbe familiar with, such as `=`, `\u003e`, `\u003c`, `\u003c=` and `\u003e=`. For array fields,\nthe `array-contains`, `IN` and `array-contains-any` operators are also\navailable.\nThis method also supports usage of Filters (see [Filter](/php/docs/reference/cloud-firestore/latest/Filter)).\nThe Filter class helps to create complex queries using AND and OR operators.\n\nExample: \n\n $query = $query-\u003ewhere('firstName', '=', 'John');\n\n // Filtering against `null` and `NAN` is supported only with the equality operator.\n $query = $query-\u003ewhere('coolnessPercentage', '=', NAN);\n\n // Use `array-contains` to select documents where the array contains given elements.\n $query = $query-\u003ewhere('friends', 'array-contains', ['Steve', 'Sarah']);\n\n use Google\\Cloud\\Firestore\\Filter;\n\n // Filtering with Filter::or\n $query = $query-\u003ewhere(Filter::or([\n Filter::field('firstName', '=', 'John'),\n Filter::field('firstName', '=', 'Monica')\n ]));\n\n### orderBy\n\nAdd an ORDER BY clause to the Query.\n\nExample: \n\n $query = $query-\u003eorderBy('firstName', 'DESC');\n\n### limit\n\nLimits a query to return only the first matching documents.\n\nApplies after all other constraints. Must be \\\u003e= 0 if specified.\n\nExample: \n\n $query = $query-\u003elimit(10);\n\n### limitToLast\n\nLimits a query to return only the last matching documents.\n\nApplies after all other constraints. Must be \\\u003e= 0 if specified.\n\nYou must specify at least one orderBy clause for limitToLast queries,\notherwise an exception will be thrown during execution.\n\nExample: \n\n $query = $query-\u003elimitToLast(10)\n -\u003eorderBy('firstName');\n\n### offset\n\nThe number of results to skip.\n\nApplies before limit, but after all other constraints. Must be \\\u003e= 0 if specified.\n\nExample: \n\n $query = $query-\u003eoffset(10);\n\n### startAt\n\nA starting point for the query results.\n\nStarts results at the provided set of field values relative to the order\nof the query. The order of the provided values must match the order of\nthe order by clauses of the query. Values in the given cursor will be\nincluded in the result set, if found.\n\nMultiple invocations of this call overwrite previous calls. Calling\n`startAt()` will overwrite both previous `startAt()` and `startAfter()`\ncalls.\n\nExample: \n\n $query = $query-\u003eorderBy('age', 'ASC')-\u003estartAt([18]);\n $users18YearsOrOlder = $query-\u003edocuments();\n\n### startAfter\n\nA starting point for the query results.\n\nStarts results after the provided set of field values relative to the order\nof the query. The order of the provided values must match the order of\nthe order by clauses of the query. Values in the given cursor will not be\nincluded in the result set.\n\nMultiple invocations of this call overwrite previous calls. Calling\n`startAt()` will overwrite both previous `startAt()` and `startAfter()`\ncalls.\n\nExample: \n\n $query = $query-\u003eorderBy('age', 'ASC')-\u003estartAfter([17]);\n $users18YearsOrOlder = $query-\u003edocuments();\n\n### endBefore\n\nAn end point for the query results.\n\nEnds results before the provided set of field values relative to the order\nof the query. The order of the provided values must match the order of\nthe order by clauses of the query. Values in the given cursor will be\nincluded in the result set, if found.\n\nMultiple invocations of this call overwrite previous calls. Calling\n`endBefore()` will overwrite both previous `endBefore()` and `endAt()`\ncalls.\n\nExample: \n\n $query = $query-\u003eorderBy('age', 'ASC')-\u003eendBefore([18]);\n $usersYoungerThan18 = $query-\u003edocuments();\n\n### endAt\n\nAn end point for the query results.\n\nEnds results at the provided set of field values relative to the order\nof the query. The order of the provided values must match the order of\nthe order by clauses of the query. Values in the given cursor will not be\nincluded in the result set.\n\nMultiple invocations of this call overwrite previous calls. Calling\n`endBefore()` will overwrite both previous `endBefore()` and `endAt()`\ncalls.\n\nExample: \n\n $query = $query-\u003eorderBy('age', 'ASC')-\u003eendAt([17]);\n $usersYoungerThan18 = $query-\u003edocuments();\n\n### queryHas\n\nCheck if a given constraint type has been specified on the query.\n\n### queryKey\n\nGet the constraint data from the current query.\n\nConstants\n---------\n\n### OP_LESS_THAN\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\FieldFilter\\Operator::LESS_THAN\n\n### OP_LESS_THAN_OR_EQUAL\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\FieldFilter\\Operator::LESS_THAN_OR_EQUAL\n\n### OP_GREATER_THAN\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\FieldFilter\\Operator::GREATER_THAN\n\n### OP_GREATER_THAN_OR_EQUAL\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\FieldFilter\\Operator::GREATER_THAN_OR_EQUAL\n\n### OP_EQUAL\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\FieldFilter\\Operator::EQUAL\n\n### OP_ARRAY_CONTAINS\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\FieldFilter\\Operator::ARRAY_CONTAINS\n\n### OP_NAN\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\UnaryFilter\\Operator::IS_NAN\n\n### OP_NULL\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\UnaryFilter\\Operator::IS_NULL\n\n### DIR_ASCENDING\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\Direction::ASCENDING\n\n### DIR_DESCENDING\n\n Value: \\Google\\Cloud\\Firestore\\V1\\StructuredQuery\\Direction::DESCENDING\n\n### DOCUMENT_ID\n\n Value: '__name__'"]]