-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
meta.txt
106 lines (75 loc) · 2.91 KB
/
meta.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
=====
$meta
=====
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. Written in anticipation of future availability of other metadata
.. projection:: $meta
.. versionadded:: 2.6
The :projection:`$meta` projection operator returns for each
matching document the metadata (e.g. ``"textScore"``) associated
with the query.
.. |meta-object| replace:: :projection:`$meta`
.. |empty-value| replace:: 0
.. include:: /includes/fact-meta-syntax.rst
Behaviors
---------
The :projection:`$meta` expression can be a part of the
:term:`projection` document as well as a :method:`~cursor.sort()`
expression as:
.. code-block:: javascript
{ <projectedFieldName>: { $meta: "textScore" } }
.. include:: /includes/extracts/views-unsupported-projection-$meta.rst
Projected Field Name
~~~~~~~~~~~~~~~~~~~~
The ``<projectedFieldName>`` cannot include a dot (``.``) in the name.
If the specified ``<projectedFieldName>`` already exists in the
matching documents, in the result set, the existing fields will return
with the :projection:`$meta` values instead of with the stored values.
Projection
~~~~~~~~~~
The :projection:`$meta` expression can be used in the
:term:`projection` document, as in:
.. code-block:: javascript
db.collection.find(
<query>,
{ score: { $meta: "textScore" } }
)
The :projection:`$meta` expression specifies the inclusion of the field
to the result set and does *not* specify the exclusion of the other
fields.
The :projection:`$meta` expression can be a part of a projection
document that specifies exclusions of other fields or that specifies
inclusions 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 a
non-existing field if no field exists in the documents with the
``<projectedFieldName>``.
Sort
~~~~
The :projection:`$meta` expression can be part of a
:method:`~cursor.sort()` expression, as in:
.. code-block:: javascript
db.collection.find(
<query>,
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
To include a :projection:`$meta` expression in a
:method:`~cursor.sort()` expression, the *same* :projection:`$meta`
expression, including the ``<projectedFieldName>``, must appear in the
projection document. The specified metadata determines the sort order.
For example, the ``"textScore"`` metadata sorts in descending order.
For additional examples, see :ref:`text-operator-example-compound-sort`.
Examples
--------
.. TODO when $meta returns other data, subsection Examples section
by keywords.
For examples of ``"textScore"`` projections and sorts, see
:query:`$text`.