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.
Sintaxis
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 } }
Campos
La etapa $searchMeta procesa un documento con los siguientes campos:
Campo | Tipo | Necesidad | Descripción |
|---|---|---|---|
| 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 |
| 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 |
| 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. |
| string | Opcional | Nombre del índice de búsqueda de MongoDB que se utilizará. Si se omite, es por defecto 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. |
| 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 |
| Objeto | Opcional | Object that sets the context of the query to the specified embedded document field. You must also specify |
| 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 Para obtener más información, consulta Devolver los campos de origen almacenados. |
Comportamiento
The $searchMeta stage must be the first stage in any pipeline.
Tipos de resultados de metadatos
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 |
|---|---|
| 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. |
| 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. |
Ejemplo
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") } } ]
Solución de problemas
If you are experiencing issues with your MongoDB Search $search queries, see Troubleshoot Queries.