-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcursor.hint.txt
133 lines (82 loc) · 2.98 KB
/
cursor.hint.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
.. _cursor-hint:
=============
cursor.hint()
=============
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: cursor.hint(index)
.. include:: /includes/fact-mongosh-shell-method.rst
Call this method on a query to override MongoDB's default index
selection and :ref:`query optimization process <read-operations-query-optimization>`.
Use :method:`db.collection.getIndexes()` to return the list of
current indexes on a collection.
The :method:`cursor.hint()` method has the following parameter:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``index``
- string or document
- The index to "hint" or force MongoDB to use when performing the query.
Specify the index either by the index name or by the index
specification document.
You can also specify ``{ $natural : 1 }`` to force the query to perform a
forwards collection scan, or ``{ $natural : -1 }`` for a reverse
collection scan.
Compatibility
-------------
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-all.rst
.. include:: /includes/fact-environments-onprem-only.rst
Behavior
--------
- When an :ref:`index filter <index-filters>` exists for the query
shape, MongoDB ignores the :method:`~cursor.hint()`.
- .. include:: /includes/fact-hint-text-query-restriction.rst
- If you use :method:`~cursor.hint()` on a :doc:`hidden index
</core/index-hidden>` or an index that doesn't exist, the operation
returns an error.
- On a :term:`time series collections <time series collection>`, you
can only specify hints using the index name, not the index key
pattern.
$natural
~~~~~~~~
.. operator:: $natural
Use ``$natural`` in conjunction with ``cursor.hint()`` to perform a
collection scan to return documents in :term:`natural order`.
For usage, see :ref:`hint-collection-scans`.
Examples
--------
Specify an Index
~~~~~~~~~~~~~~~~
The following example returns all documents in the collection named
``users`` using the index on the ``age`` field.
.. code-block:: javascript
db.users.find().hint( { age: 1 } )
You can also specify the index using the index name:
.. code-block:: javascript
db.users.find().hint( "age_1" )
.. _hint-collection-scans:
Force Collection Scans
~~~~~~~~~~~~~~~~~~~~~~
You can specify ``{ $natural : 1 }`` to force the query to perform a forwards
collection scan:
.. code-block:: javascript
db.users.find().hint( { $natural : 1 } )
You can also specify ``{ $natural : -1 }`` to force the query to perform a
reverse collection scan:
.. code-block:: javascript
db.users.find().hint( { $natural : -1 } )
.. seealso::
- :doc:`/indexes`
- :doc:`/core/query-plans`
- :ref:`index-filters`