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