- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $meta
$meta¶
On this page
Definition¶
-
$meta
¶
-
$meta
¶ Returns the metadata associated with a document, e.g.
"textScore"
when performing text search.A
$meta
expression has the following syntax:The
$meta
expression can specify the following values as the<metaDataKeyword>
:Keyword Description "textScore"
Returns the score associated with the corresponding
$text
query for each matching document. The text score signifies how well the document matched the search term or terms.Starting in MongoDB 4.4, must be used in conjunction with a
$text
query.In earlier versions, if not used in conjunction with a
$text
query, 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()
.New in version 4.4.
MongoDB Atlas Search provides additional
$meta
keywords, such as:Refer to the Atlas Search documentation for details.
Behavior¶
Text Score Metadata $meta: "textScore"
¶
Requires $text Search
|
Availability
|
Usage In Projection
|
Filter on Text Score
|
Usage In Sort
|
Sort without Projection
|
Sort with Projection
|
Index Key Metadata $meta: “indexKey” (Aggregation and Find)¶
Usage
|
Availability
|
Return Value
|
Examples¶
$meta: "textScore"
¶
Create an articles
collection with the following documents:
Create a text index on the title
field:
- Aggregation
- Find and Project
The following aggregation operation performs a text search and uses the
$meta
operator to group by the text search score:
The operation returns the following results:
For more examples, see Text Search in the Aggregation Pipeline.
The following query performs a text search for the term cake
and
uses the $meta
operator in the projection document to
include the score assigned to each matching document:
The operation returns the following documents with the text score:
For additional examples of "textScore"
projections and sorts,
see Text Search Score Examples.
$meta: "indexKey"
¶
Note
The { $meta: "indexKey" }
expression is for debugging
purposes only and not for application logic. MongoDB returns the
value associated with the index chosen by the query system. The
system can choose a different index upon subsequent execution.
For the selected index, 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.
Create an orders
collection with the following documents:
Create the following compound index on the type
and item
fields:
- Aggregation
- Find and Project
The following aggregation operation finds all documents with type
equal to apparel
and uses the $meta
operator to
include the index key value for the matching document if an index was
used:
The following operation finds all documents with type
equal to apparel
and uses the $meta
operator to
include the index key value for the matching document if an index was
used:
The operation returns the matching documents with their corresponding index key:
If no index is used, the { $meta: "indexKey" }
does not
return anything.
- Aggregation
- Find and Project
For example, the following operation does not use
an index since no index exists on the price
field to support the
match condition:
For example, the following operation does not use an index
since no index exists on the price
field to support the
match condition:
The operation returns the matching documents without the idxKey
field: