Skip to content

Commit e4b1a61

Browse files
(DOCS-13305): meta and natural tweaks for 4.4
1 parent 93e0667 commit e4b1a61

File tree

6 files changed

+145
-16
lines changed

6 files changed

+145
-16
lines changed

source/includes/extracts-4.4-changes.yaml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,107 @@ content: |
132132
Starting in MongoDB 4.4, |timestampfmt| no longer supports ``ctime``.
133133
An example of ``ctime`` formatted date is: ``Wed Dec 31
134134
18:17:54.811``.
135+
---
136+
ref: 4.4-changes-projection-sort-meta
137+
content: |
138+
139+
Starting in MongoDB 4.4:
140+
141+
- You can specify :projection:`$meta` in the projection document
142+
of a :dbcommand:`find` command without specifying the
143+
:projection:`$meta` expression as part of a
144+
:method:`~cursor.sort()` document:
145+
146+
.. code-block:: javascript
147+
148+
db.collection.find(
149+
<query>,
150+
{ score: { $meta: "textScore" } }
151+
)
152+
153+
As a result, you can project the
154+
:ref:`textScore <text-operator-text-score>` metadata without
155+
sorting the results by their search relevance.
156+
157+
- You can specify the :projection:`$meta` expression as part of
158+
a :method:`~cursor.sort()` document without specifying
159+
:projection:`$meta` in the projection document of a
160+
:dbcommand:`find` command:
161+
162+
.. code-block:: javascript
163+
164+
db.collection.find(
165+
<query>
166+
).sort( { score: { $meta: "textScore" } } )
167+
168+
As a result, you can sort the resulting documents by their
169+
search relevance without projecting the ``textScore``.
170+
171+
- If you specify the :projection:`$meta` expression as part of a
172+
:method:`~cursor.sort()` document and the :term:`projection`
173+
document of a :dbcommand:`find` command, you can specify different
174+
field names for the :method:`~cursor.sort()` document and the
175+
:term:`projection` document.
176+
177+
In the following example, the ``score`` field associated with the
178+
:projection:`$meta` expression in the projection document contains
179+
the document's :ref:`textScore <text-operator-text-score>`
180+
metadata. In contrast, the ``ignoredName`` field in the
181+
:method:`~cursor.sort()` document has an arbitrary name which
182+
does not affect the query.
183+
184+
.. code-block:: javascript
185+
186+
db.collection.find(
187+
<query>,
188+
{ score: { $meta: "textScore" } }
189+
).sort( { ignoredName: { $meta: "textScore" } } )
190+
191+
In previous versions of MongoDB, the *same* :projection:`$meta`
192+
expression, including the ``<projectedFieldName>``, must appear in
193+
the :method:`~cursor.sort()` document and the :dbcommand:`find`
194+
command's projection document.
195+
---
196+
ref: 4.4-changes-textscore-predicate
197+
content: |
198+
199+
Starting in MongoDB 4.4, you must specify the :query:`$text` operator
200+
in the query predicate to use ``{ $meta: "textScore" }`` in either
201+
the :term:`projection` document or a :method:`~cursor.sort()`
202+
document.
203+
204+
If you do not specify the :query:`$text` operator in the query
205+
predicate, the operation fails.
206+
207+
.. example::
208+
209+
The following :dbcommand:`find` command fails because it specifies
210+
``{ $meta: "textScore" }`` in the :term:`projection` document, but
211+
does not specify :query:`$text` operator in the query predicate:
212+
213+
.. code-block:: javascript
214+
:copyable: false
215+
216+
// This command fails:
217+
218+
db.articles.find( { }, { score: { $meta: "textScore" } } )
219+
220+
To successfully execute the command, include the :query:`$text`
221+
operator in the query predicate:
222+
223+
.. code-block:: javascript
224+
225+
// This command succeeds:
226+
227+
db.articles.find(
228+
{ $text: { $search: "coffee" } },
229+
{ score: { $meta: "textScore" } }
230+
)
231+
---
232+
ref: 4.4-changes-natural-sort-views
233+
content: |
135234
235+
Starting in MongoDB 4.4, you can specify a :operator:`$natural`
236+
sort when running a :dbcommand:`find` operation against a
237+
:doc:`view </core/views>`.
136238
...

source/reference/method/cursor.sort.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,8 @@ Return in Natural Order
262262

263263
The :operator:`$natural` parameter returns items according to their
264264
:term:`natural order` within the database. This ordering is an internal
265-
implementation feature, and you should not rely on any particular structure
266-
within it.
267-
268-
.. note::
269-
270-
.. include:: /includes/extracts/views-unsupported-natural-sort.rst
265+
implementation feature, and you should not rely on any particular
266+
ordering of the documents.
271267

272268
Index Use
273269
~~~~~~~~~

source/reference/operator/meta/natural.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Definition
1010

1111
.. operator:: $natural
1212

13-
.. versionchanged:: 3.2
13+
.. versionchanged:: 4.4
1414

1515
Use in conjunction with :method:`cursor.hint()` to perform a
1616
collection scan to return documents in :term:`natural order`.
1717

1818
For usage, see :ref:`hint-collection-scans` example in the
1919
:method:`cursor.hint()` reference page.
2020

21-
.. include:: /includes/extracts/views-unsupported-natural-sort.rst
21+
.. include:: /includes/extracts/4.4-changes-natural-sort-views.rst

source/reference/operator/projection/meta.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ expression as:
3333

3434
{ <projectedFieldName>: { $meta: "textScore" } }
3535

36-
.. include:: /includes/extracts/views-unsupported-projection-$meta.rst
36+
:query:`$text` search is not fully supported by read-only
37+
non-materialized :doc:`views </core/views>`. In particular,
38+
:method:`db.collection.find()` operations on views do not support
39+
``{ $meta: "textScore" }``.
3740

3841
Projected Field Name
3942
~~~~~~~~~~~~~~~~~~~~
@@ -77,7 +80,7 @@ Sort
7780
~~~~
7881

7982
The :projection:`$meta` expression can be part of a
80-
:method:`~cursor.sort()` expression, as in:
83+
:method:`~cursor.sort()` document, as in:
8184

8285
.. code-block:: javascript
8386

@@ -86,14 +89,23 @@ The :projection:`$meta` expression can be part of a
8689
{ score: { $meta: "textScore" } }
8790
).sort( { score: { $meta: "textScore" } } )
8891

89-
To include a :projection:`$meta` expression in a
90-
:method:`~cursor.sort()` expression, the *same* :projection:`$meta`
91-
expression, including the ``<projectedFieldName>``, must appear in the
92-
projection document. The specified metadata determines the sort order.
92+
The specified metadata determines the sort order.
9393
For example, the ``"textScore"`` metadata sorts in descending order.
9494

95+
Projection and Sort Behavior
96+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
.. include:: /includes/extracts/4.4-changes-projection-sort-meta.rst
99+
95100
For additional examples, see :ref:`text-operator-example-compound-sort`.
96101

102+
.. _meta-text-predicate:
103+
104+
Query Predicate Must Include ``$text``
105+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106+
107+
.. include:: /includes/extracts/4.4-changes-textscore-predicate.rst
108+
97109
Examples
98110
--------
99111

source/release-notes/4.4-compatibility.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,16 @@ That is, the method no longer accepts
8080
Projection Restrictions
8181
-----------------------
8282

83-
.. include:: /includes/fact-projection-path-collision.rst
83+
- .. include:: /includes/fact-projection-path-collision.rst
84+
85+
``$meta`` Behavior Changes
86+
--------------------------
87+
88+
- Starting in MongoDB 4.4, when using :projection:`$meta` as a
89+
projection operator or as part of a :method:`~cursor.sort()`
90+
expression, you must specify the :query:`$text` operator in the
91+
query predicate to use ``{ $meta: "textScore" }``. See
92+
:ref:`meta-text-predicate` for an example.
8493

8594
General Changes
8695
---------------

source/release-notes/4.4.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,17 @@ db.auth() Can Prompt for Password
413413
Starting in MongoDB 4.4, the :binary:`~bin.mongo` shell method
414414
:ref:`db-auth-syntax-username-password` prompts for the password if
415415
you do not pass in the password or the :method:`passwordPrompt()`
416-
method for the ``<password>``.
416+
method for the ``<password>``.
417+
418+
Support for ``$natural`` Sort on Views
419+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
420+
421+
.. include:: /includes/extracts/4.4-changes-natural-sort-views.rst
422+
423+
Validation Changes for ``$meta``
424+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
425+
426+
.. include:: /includes/extracts/4.4-changes-projection-sort-meta.rst
417427

418428
:doc:`Changes Affecting Compatibility </release-notes/4.4-compatibility>`
419429
-------------------------------------------------------------------------

0 commit comments

Comments
 (0)