Definition
$metaReturns the metadata associated with a document, e.g.
"textScore"when performing text search.A
$metaexpression has the following syntax:{ $meta: <metaDataKeyword> } The
$metaexpression can specify the following values as the<metaDataKeyword>:KeywordDescription"textScore"Returns the score associated with the corresponding
$textquery for each matching document. The text score signifies how well the document matched the search term or terms.{ $meta: "textScore" }must be used in conjunction with a$textquery.In earlier versions, if not used in conjunction with a
$textquery, returns a score of null."indexKey"Returns an index key for the document if a non-text index is used. The
{ $meta: "indexKey" }expression is for debugging purposes only, and not for application logic, and is preferred overcursor.returnKey().MongoDB Search provides additional
$metakeywords, such as:Refer to the MongoDB Search documentation for details.
Important
The following
$metakeywords are not supported in Stable API V1:"textScore""indexKey""score""searchScore""scoreDetails""searchHighlights""searchSequenceToken""searchScoreDetails""vectorSearchScore""searchRootDocumentId"
You can use
searchScoreonly in$searchqueries andvectorSearchScoreonly in$vectorSearchqueries. If you usesearchScoreandvectorSearchScorein any other query, that query will fail.You can use
searchRootDocumentIdonly in$searchqueries withreturnScope.
Behavior
Text Score Metadata $meta: "textScore"
Requires $text Search
The
{ $meta: "textScore" }expression must be used in conjunction with$text. For example:In find, you must specify the
$textoperator in the query predicate to use{ $meta: "textScore" }. If you do not specify the$textoperator in the query predicate, the operation fails.
Note
$textprovides text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search.
Availability
In find, the
{ $meta: "textScore" }expression can be included in projection and insort().
Usage In Projection
The
{ $meta: "textScore" }expression can be a part of the projection document to include the text score metadata.The
$metaexpression can be present in either an inclusion or an exclusion projection.If you set the expression to a field name that already exists in the document, the projected metadata value overwrites the existing value.
Filter on Text Score
In aggregation, following a stage that outputs a field with the text score value, you can specify a query condition or operate on the field in subsequent stages. For example, see $text in the Aggregation Pipeline.
In find, you cannot specify a query condition on the text score. Use aggregation instead.
Usage In Sort
The
"textScore"metadata sorts in descending order.To use in a sort operation, set the
{ $meta: "textScore" }expression to an arbitrary field name. The field name is disregarded by the query system.
Sort without Projection
In aggregation, you can sort the resulting documents by
{ $meta: "textScore" }without also having to project thetextScore.In find, you can sort the resulting documents by
{ $meta: "textScore" }without also having to project thetextScore.
Sort with Projection
In aggregation, if you include the
{ $meta: "textScore" }expression in both the projection and sort, the projection and sort can have different field names for the expression. The field name in the sort is disregarded by the query system.In find, if you include the
{ $meta: "textScore" }expression in both the projection and sort, the projection and sort can have different field names for the expression. The field name in the sort is disregarded by the query system.
Index Key Metadata $meta: "indexKey" (Aggregation and Find)
Usage
The
{ $meta: "indexKey" }expression is for debugging purposes only and not for application logic.The
{ $meta: "indexKey" }expression is preferred overcursor.returnKey().
Availability
In aggregation, the
{ $meta: "indexKey" }expression can be included in various stages that accept aggregation expressions, such as$project,$group$sortByCount, etc., but not$sort. However, with an aggregation pipeline, you can first project the{ $meta: "indexKey" }expression (such as in a$project,$addFields, etc. ) and then, sort by that field in a subsequent$sortstage.In find, the
{ $meta: "indexKey" }expression is only available as part of the projection document.
Return Value
The value returned depends on how the database decides to represent values in an index and may change across versions. The represented value may not be the actual value for the field.
The value returned depends on the execution plan chosen by the system. For example, if there are two possible indexes which can be used to answer the query, then the value of the "indexKey" metadata depends on which index is selected.
If an index is not used, the
{ $meta: "indexKey" }expression does not return a value and the field is not included as part of the output.