Skip to content

natural disables index use #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
4 changes: 4 additions & 0 deletions source/reference/method/cursor.sort.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ The query returns the following documents, ordered first by the
{ "_id" : 2, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 50 }
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }

.. _return-storage-order:

Return in Storage Order
-----------------------

Expand All @@ -191,6 +193,8 @@ documents relocate because of :ref:`document growth due to updates
<data-model-document-growth>` or remove operations free up space which
are then taken up by newly inserted documents.

.. include:: /includes/fact-natural-parameter.rst

Consider the sequence of insert operations to the ``trees`` collection:

.. code-block:: javascript
Expand Down
118 changes: 112 additions & 6 deletions source/reference/operator/meta/natural.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ $natural

.. default-domain:: mongodb

Definition
----------

.. operator:: $natural

Use the :operator:`$natural` operator to use :term:`natural order` for
the results of a sort operation. Natural order refers to the
order of documents in the file on disk.
:ref:`storage order <return-storage-order>` of documents in the file on disk.

The :operator:`$natural` operator uses the following syntax to return
documents in the order they exist on disk:
Expand All @@ -17,13 +20,116 @@ $natural

db.collection.find().sort( { $natural: 1 } )

Use ``-1`` to return documents in the reverse order as they occur on
disk:
Behavior
--------

On a sharded collection the :operator:`$natural` operator returns a
collection scan sorted in :ref:`storage order<return-storage-order>`, the
order the database inserts and stores documents on disk.

.. include:: /includes/fact-natural-parameter.rst

.. include:: /includes/fact-natural-sort-order-text-query-restriction.rst

Examples
--------

Reverse Order
~~~~~~~~~~~~~

Use ``-1`` to return documents in the reverse order as they occur on disk:

.. code-block:: javascript

db.collection.find().sort( { $natural: -1 } )

Natural Order Comparison
~~~~~~~~~~~~~~~~~~~~~~~~

In this scenario:

- Create an extra index as { normal: 1 }.

- Insert relevant objects with ``_id`` and ``normal`` values, for example,
a string type object created with:

.. code-block:: javascript

db.collection.find().sort( { $natural: -1 } )
db.coll.insert( { _id: "01", normal: "01" } )

- Use a different type for member values for each distinct object, with the
same values in each object.

- Use ``.find().sort().explain()`` for all operations.

This scenario returns these results when using
:method:`~db.collection.find()` for different ``_id`` values; sorting with
the :operator:`$natural` operator, ``_id`` index, and ``normal`` index;
and a description of the :method:`~cursor.explain()` method output:

.. list-table::
:header-rows: 2
:widths: 20 25 25 25

* -
-
- :method:`~cursor.sort()`
-

* - :method:`~db.collection.find()`
- :operator:`$natural`:1
- :term:`_id`:1
- normal:1

* - :term:`_id`::doc:`ObjectId() </reference/bson-types>`

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

* - :term:`_id`::doc:`Object() </reference/bson-types>`

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

* - :term:`_id`::doc:`string() </reference/bson-types>`

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

* - :term:`_id`::doc:`integer() </reference/bson-types>`

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

* - :term:`_id`::doc:`BinData() </reference/bson-types>`

- :method:`~cursor.explain()` scanned entire collection

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

* - normal:(any query)

- :method:`~cursor.explain()` scanned entire collection

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

- :method:`~cursor.explain()` used :term:`B-Tree` cursor

.. include:: /includes/fact-natural-sort-order-text-query-restriction.rst
Additional Information
----------------------

.. seealso:: :method:`cursor.sort()`
:method:`cursor.sort()`