@@ -43,15 +43,77 @@ fields:
43
43
44
44
:method:`db.collection.createIndex()`
45
45
46
- Index Hints for Time Series Collections
47
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
+ .. _timeseries-secondary-index-sort-performance:
48
47
49
- To force MongoDB to use a specific index when querying a :term:`time
50
- series collection`, use :method:`cursor.hint()` with an index name:
48
+ Use Secondary Indexes to Improve Sort Performance
49
+ -------------------------------------------------
50
+
51
+ Sort operations on the ``timeField`` and ``metaField`` can use secondary
52
+ indexes on those fields to improve performance.
53
+
54
+ For example, the following ``sensorData`` collection contains
55
+ temperature readings:
56
+
57
+ .. code-block:: javascript
58
+
59
+ db.sensorData.insertMany( [
60
+ {
61
+ "metadata": { "sensorId": 5578, "type": "temperature" },
62
+ "timestamp": ISODate("2022-01-15T00:00:00.000Z"),
63
+ "temperatureReading": 12
64
+ },
65
+ {
66
+ "metadata": { "sensorId": 5578, "type": "temperature" },
67
+ "timestamp": ISODate("2022-01-15T04:00:00.000Z"),
68
+ "temperatureReading": 11
69
+ },
70
+ {
71
+ "metadata": { "sensorId": 5579, "type": "temperature" },
72
+ "timestamp": ISODate("2022-01-15T08:00:00.000Z"),
73
+ "temperatureReading": 9
74
+ }
75
+ ] )
76
+
77
+ The following command creates a compound ascending secondary index on
78
+ the ``timestamp`` and ``metadata.sensorId`` fields:
79
+
80
+ .. code-block:: javascript
81
+
82
+ db.sensorData.createIndex(
83
+ { "timestamp": 1, "metadata.sensorId": 1 }
84
+ )
85
+
86
+ The following sort operation on the ``timestamp`` field uses the index
87
+ to improve performance:
88
+
89
+ .. code-block:: javascript
90
+
91
+ db.sensorData.find().sort( { "timestamp": 1 } )
92
+
93
+ To confirm that the sort operation used the index, run the operation
94
+ again with the ``.explain()`` option:
51
95
52
96
.. code-block:: javascript
53
97
54
- db.weather24h.find({ "metadata.sensorId": 1235 }).hint("metadata.sensorId_1_timestamp_1")
98
+ db.sensorData.find().sort( { "timestamp": 1 } ).explain()
99
+
100
+ The ``winningPlan.queryPlan.inputStage.stage`` is ``IXSCAN``, which
101
+ indicates that the index was used. For more information on explain plan
102
+ output, see :ref:`explain-results`.
103
+
104
+ Specify Index Hints for Time Series Collections
105
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106
+
107
+ Index hints cause MongoDB to use a specific index for a query. Some
108
+ operations on time series collections can only take advantage of an
109
+ index if that index is specified in a hint.
110
+
111
+ For example, the following query causes MongoDB to use the
112
+ ``timestamp_1_metadata.sensorId_1`` index:
113
+
114
+ .. code-block:: javascript
115
+
116
+ db.sensorData.find( { "metadata.sensorId": 5578 } ).hint( "timestamp_1_metadata.sensorId_1" )
55
117
56
118
On a time series collection, you can specify hints using either the
57
119
index name or the index key pattern. To get the names of the indexes on
@@ -60,7 +122,7 @@ a collection, use the :method:`db.collection.getIndexes()` method.
60
122
.. _timeseries-add-secondary-index-mongodb-6.0:
61
123
62
124
Time Series Secondary Indexes in MongoDB 6.0
63
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
+ --------------------------------------------
64
126
65
127
Starting in MongoDB 6.0:
66
128
@@ -78,29 +140,3 @@ Starting in MongoDB 6.0:
78
140
.. note::
79
141
80
142
.. include:: /includes/time-series-secondary-indexes-downgrade-FCV.rst
81
-
82
- For example, the following ``sensorData`` collection contains
83
- temperature readings:
84
-
85
- .. code-block:: javascript
86
-
87
- db.sensorData.insertMany( [
88
- {
89
- "metadata": { "sensorId": 5578, "type": "temperature" },
90
- "timestamp": ISODate("2022-01-15T00:00:00.000Z"),
91
- "temperatureReading": 12
92
- },
93
- {
94
- "metadata": { "sensorId": 5578, "type": "temperature" },
95
- "timestamp": ISODate("2022-01-15T04:00:00.000Z"),
96
- "temperatureReading": 11
97
- }
98
- ] )
99
-
100
- The following example creates an ascending secondary index on the
101
- ``metadata.sensorId`` and ``temperatureReading`` fields in the
102
- ``sensorData`` collection:
103
-
104
- .. code-block:: javascript
105
-
106
- db.sensorData.createIndex( { "metadata.sensorId": 1, "temperatureReading": 1 } )
0 commit comments