@@ -212,3 +212,56 @@ Round Numeric Data to Few Decimal Places
212
212
Round numeric data to the precision required for your application.
213
213
Rounding numeric data to fewer decimal places improves the compression
214
214
ratio.
215
+
216
+ Optimize Query Performance
217
+ --------------------------
218
+
219
+ Query metaFields on Sub-Fields
220
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221
+
222
+ MongoDB reorders the metaFields of time-series collections, which may
223
+ cause servers to store data in a different field order than
224
+ applications. If metaFields are objects, queries on entire metaFields
225
+ may produce inconsistent results because metaField order may vary
226
+ between servers and applications. To optimize queries on time-series
227
+ metaFields, query timeseries metaFields on scalar sub-fields rather than
228
+ entire metaFields.
229
+
230
+ The following example creates a time series collection:
231
+
232
+ .. code-block:: javascript
233
+
234
+ db.weather.insertMany( [
235
+ {
236
+ "metaField": { "sensorId": 5578, "type": "temperature" },
237
+ "timestamp": ISODate( "2021-05-18T00:00:00.000Z" ),
238
+ "temp": 12
239
+ },
240
+ {
241
+ "metaField": { "sensorId": 5578, "type": "temperature" },
242
+ "timestamp": ISODate( "2021-05-18T04:00:00.000Z" ),
243
+ "temp": 11
244
+ }
245
+ ] )
246
+
247
+ The following query on the ``sensorId`` and ``type`` scalar sub-fields
248
+ returns the first document that matches the query criteria:
249
+
250
+ .. code-block:: javascript
251
+
252
+ db.weather.findOne( {
253
+ "metaField.sensorId": 5578,
254
+ "metaField.type": "temperature"
255
+ } )
256
+
257
+ Example output:
258
+
259
+ .. code-block:: javascript
260
+ :copyable: false
261
+
262
+ {
263
+ _id: ObjectId("6572371964eb5ad43054d572"),
264
+ metaField: { sensorId: 5578, type: 'temperature' },
265
+ timestamp: ISODate( "2021-05-18T00:00:00.000Z" ),
266
+ temp: 12
267
+ }
0 commit comments