$meta

  • $meta

New in version 2.6.

The $meta projection operator returns for eachmatching document the metadata (e.g. "textScore") associatedwith the query.

A $meta expression has the following syntax:

  1. { $meta: <metaDataKeyword> }

The $meta expression can specify the following keywordas the <metaDataKeyword>:

KeywordDescriptionSort Order"textScore"Returns the score associated with the corresponding$text query for each matching document. The text scoresignifies how well the document matched the search term orterms. If not used inconjunction with a $text query, returns a score of0.Descending

MongoDB Atlas Full-Text Search providesadditional $meta keywords, such as:

Behaviors

The $meta expression can be a part of theprojection document as well as a sort()expression as:

  1. { <projectedFieldName>: { $meta: "textScore" } }

db.collection.find() operations on views do not support $meta projection operator.

Projected Field Name

The <projectedFieldName> cannot include a dot (.) in the name.

If the specified <projectedFieldName> already exists in thematching documents, in the result set, the existing fields will returnwith the $meta values instead of with the stored values.

Projection

The $meta expression can be used in theprojection document, as in:

  1. db.collection.find(
  2. <query>,
  3. { score: { $meta: "textScore" } }
  4. )

The $meta expression specifies the inclusion of the fieldto the result set and does not specify the exclusion of the otherfields.

The $meta expression can be a part of a projectiondocument that specifies exclusions of other fields or that specifiesinclusions of other fields.

The metadata returns information on the processing of the <query>operation. As such, the returned metadata, assigned to the<projectedFieldName>, has no meaning inside a <query>expression; i.e. specifying a condition on the <projectedFieldName>as part of the <query> is similar to specifying a condition on anon-existing field if no field exists in the documents with the<projectedFieldName>.

Sort

The $meta expression can be part of asort() expression, as in:

  1. db.collection.find(
  2. <query>,
  3. { score: { $meta: "textScore" } }
  4. ).sort( { score: { $meta: "textScore" } } )

To include a $meta expression in asort() expression, the same$metaexpression, including the <projectedFieldName>, must appear in theprojection document. The specified metadata determines the sort order.For example, the "textScore" metadata sorts in descending order.

For additional examples, see Text Search with Additional Query and Sort Expressions.

Examples

For examples of "textScore" projections and sorts, see$text.