Docs Menu
Docs Home
/ /

$searchMeta

$searchMeta

The $searchMeta stage returns different types of metadata result documents.

Nota

To run $searchMeta queries over sharded collections, your cluster must run MongoDB v6.0 or later.

Una etapa de pipeline $searchMeta tiene la siguiente forma de prototipo:

{
$searchMeta: {
"index": "<index-name>",
"<collector-name>"|"<operator-name>": {
<collector-specification>|<operator-specification>
},
"concurrent": true | false,
"count": {
<count-options>
},
"returnScope": {
"path": "<embedded-documents-field-to-retrieve>"
}
"returnStoredSource": true | false
}
}

La etapa $searchMeta procesa un documento con los siguientes campos:

Campo
Tipo
Necesidad
Descripción

<collector-name>

Objeto

Condicional

Name of the collector to use with the query. You can provide a document that contains the collector-specific options as the value for this field. Value must be facet to retrieve a mapping of the defined facet names to an array of buckets for that facet. To learn more, see facet (MongoDB Search Operator). You must specify this or <operator-name>.

concurrent

booleano

Opcional

Paralelice la búsqueda entre segmentos en nodos de búsqueda dedicados. Si no tiene nodos de búsqueda separados en su clúster, la búsqueda de MongoDB ignora ese indicador. Si se omite, es por defecto false. Para aprender más, consulte Paralelización de la ejecución de queries entre segmentos.

count

Objeto

Opcional

Documento que especifica las opciones de conteo para obtener un conteo de los resultados. Para obtener más información, consulte Conteo de los resultados de la búsqueda de MongoDB.

index

string

Opcional

Nombre del índice de búsqueda de MongoDB que se utilizará. Si se omite, es por defecto default.

La búsqueda de MongoDB no devuelve resultados si escribe mal el nombre del índice o si el índice especificado no existe en el clúster.

<operator-name>

Objeto

Condicional

Name of the operator to search with. You can provide a document that contains the operator-specific options as the value for this field. You must specify this or <collector-name>. $searchMeta returns the default count metadata only.

returnScope

Objeto

Opcional

Object that sets the context of the query to the specified embedded document field. You must also specify returnStoredSource and set it to true if your cluster MongoDB version is less than 8.2.

returnStoredSource

booleano

Condicional

Flag that specifies whether to perform a full document lookup on the backend database or return only stored source fields directly from MongoDB Search. If omitted, defaults to false. Must be true to if you specify returnScope and your cluster MongoDB version is less than 8.2.

Para obtener más información, consulta Devolver los campos de origen almacenados.

The $searchMeta stage must be the first stage in any pipeline.

The structure of the metadata results document that is returned by the $searchMeta stage varies based on the type of results. MongoDB Search supports the following result types:

Tipo
Estructura de resultados

count

The count result included in the results indicate whether the count returned in the results is a total count of the search results, or a lower bound. To learn more, see Count Results.

facet

The result to a facet query is a mapping of the defined facet names to an array of buckets for that facet. To learn more, see Facet Results.

Supón el siguiente índice en la colección sample_mflix.movies.

{
"mappings": {
"dynamic": false,
"fields": {
"year": {
"type": "number"
}
}
}
}

The following query searches for the number of movies released in 1998 using the $searchMeta stage.

db.movies.aggregate([
{
"$searchMeta": {
"range": {
"path": "year",
"gte": 1998,
"lt": 1999
},
"count": {
"type": "total"
}
}
}
])
[ { count: { total: Long("552") } } ]

If you are experiencing issues with your MongoDB Search $search queries, see Troubleshoot Queries.

Volver

Documentos: $search

En esta página