counting
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset
Available tabs: other tabs: basic, facet, lowerBound, total -->
Count MongoDB Search Results
The MongoDB Search count option adds a field to the [metadata results](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#std-label-fts-metadata-results document that displays a count of the search results for the query. You can use count to determine the size of the result set. You can use it in the [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search or [$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta stage. You must use it in conjunction with the [operators](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-operators-ref or [collectors](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-collectors-ref to display either the total number of documents or a lower bound on the number of documents that match the query.
MongoDB recommends using count with the [$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta stage to retrieve metadata results only for the query. To retrieve metadata results and query results using the [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search stage, you must use the $$SEARCH_META variable. To learn more, see [SEARCH_META Aggregation Variable.](https://www.mongodb.com/docs/search/query/counting.md#std-label-fts-count-aggregation-variable
Considerations
To use the count option over sharded collections, your cluster must run MongoDB 7.0 or later. On sharded clusters running MongoDB 7.2.0, [$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta might return an error for count.
MongoDB Search doesn't include the count results in the results for queries run with count in [explain](https://www.mongodb.com/docs/search/query/explain.md#std-label-explain-ref mode.
Syntax
count has the following syntax:
{
"$searchMeta"|"$search": {
"index": "<index name>", // optional, defaults to "default"
"<operator>": {
<operator-specifications>
},
"count": {
"type": "lowerBound"|"total",
"threshold": <number-of-documents> //optional
}
}
}
Options
| Field | Type | Description | Required? |
|---|---|---|---|
type |
string | Type of count of the documents in the result set. Value can be one of the following: lowerBound - for a lower bound count of the number of documents that match the query. You can set the threshold for the lower bound number.; total - for an exact count of the number of documents that match the query. If the result set is large, MongoDB Search might take longer than for lowerBound to return the count. If omitted, defaults to lowerBound. |
no |
threshold |
int | Number of documents to include in the exact count if type is lowerBound. If omitted, defaults to 1000, which indicates that any number up to 1000 is an exact count and any number above 1000 is a rough count of the number of documents in the result. |
no |
Count Results
The count document included in the results document contains the following integer fields:
| Option | Description |
|---|---|
lowerBound |
Lower bound for this result set. This is returned by default if you omit the type option and when you explicitly request a [count](https://www.mongodb.com/docs/search/query/counting.md#std-label-count-ref of type lowerBound. |
total |
Total count for this result set. This is returned only when a [count](https://www.mongodb.com/docs/search/query/counting.md#std-label-count-ref of type total is requested. |
SEARCH_META Aggregation Variable
When you run your query using the [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search stage, MongoDB Search stores the metadata results in the $$SEARCH_META variable and returns only the search results. You can use the $$SEARCH_META variable in all the supported aggregation pipeline stages to view the metadata results for your [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search query. You can also use the $$SEARCH_META variable with [facet.aggregations.](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-aggregation
MongoDB recommends using the $$SEARCH_META variable only if you need both the search results and the metadata results. Otherwise, use the:
-
[
$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search stage for just the search results. -
[
$searchMeta](https://www.mongodb.com/docs/search/query/aggregation-stages/searchmeta.md#mongodb-pipeline-pipe.-searchMeta stage for just the metadata results.
Example:
Basic
Suppose an index on the released field in the sample_mflix.movies collection:
{
"mappings": {
"dynamic": false,
"fields": {
"released": {
"type": "date"
}
}
}
}
The following query searches for movies released near September 01, 2011 in the movies collection. The query requests a total count of the results. The query includes a:
The sample query uses the following stages:
[$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search |
Searches for movies released near September 01, 2011 in the movies collection. The query requests a total count of the results and metadata on the genres field. |
|---|---|
[$project](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project |
Excludes all fields except title and released from the documents and includes the metadata results stored in the $$SEARCH_META variable as the value of a field named meta. |
[$limit](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit.md#mongodb-pipeline-pipe.-limit |
Limits the output to 2 documents. |
db.movies.aggregate([
{
"$search": {
"near": {
"path": "released",
"origin": ISODate("2011-09-01T00:00:00.000+00:00"),
"pivot": 7776000000
},
"count": {
"type": "total"
}
}
},
{
"$project": {
"meta": "$$SEARCH_META",
"title": 1,
"released": 1
}
},
{
"$limit": 2
}
])
Output:
{
"_id" : ObjectId("573a13c3f29313caabd6b025"),
"title" : "Submarino",
"released" : ISODate("2011-09-01T00:00:00Z"),
"meta" : {
"count" : { "total" : NumberLong(23026) }
}
}
{
"_id" : ObjectId("573a13c7f29313caabd748f7"),
"title" : "Devil's Playground",
"released" : ISODate("2011-09-01T00:00:00Z"),
"meta" : {
"count" : { "total" : NumberLong(23026) }
}
}
To learn more about the results, see [Count Results.](https://www.mongodb.com/docs/search/query/counting.md#std-label-count-results
Examples
The following example uses an index on the year field in the sample_mflix.movies collection:
{
"mappings": {
"dynamic": false,
"fields": {
"year": {
"type": "number"
}
}
}
}
lowerBound Example
The following query searches for the movies between the years 2010 and 2015 in the movies collection. The query requests a lower bound count of the results:
db.movies.aggregate([
{
"$searchMeta": {
"range": {
"path": "year",
"gte": 2010,
"lte": 2015
},
"count": {
"type": "lowerBound"
}
}
}
])
MongoDB Search returns the following results:
{ "count" : { "lowerBound" : NumberLong(1001) } }
