Skip to content

Commit

Permalink
DOCS 16485 query time series meta fields (#5542)
Browse files Browse the repository at this point in the history
* DOCS-16485 time-series metafield query

* DOCS-16485 updating example

* DOCS-16485 updating example

* DOCS-16485 updating example

* DOCS-16485 updating title

* DOCS-16485 copy edits

* DOCS-16485 tech edits:

* DOCS-16485 updating limitations page

* DOCS-16485 moving example to limitations page

* DOCS-16485 moving example to best practices page and off limitations page

* DOCS-16485 fixing wording

* DOCS-16485 copy edits

* DOCS-16485 copy edits
  • Loading branch information
ltran-mdb2 authored Feb 7, 2024
1 parent e9cd3a2 commit e1d2806
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
52 changes: 51 additions & 1 deletion source/core/timeseries/timeseries-best-practices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,54 @@ To improve query performance, :ref:`create one or more secondary indexes
<timeseries-add-secondary-index>` on your ``timeField`` and
``metaField`` to support common query patterns. In versions 6.3 and
higher, MongoDB creates a secondary index on the ``timeField`` and
``metaField`` automatically.
``metaField`` automatically.

Query metaFields on Sub-Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MongoDB reorders the metaFields of time-series collections, which may
cause servers to store data in a different field order than
applications. If metaFields are objects, queries on entire metaFields
may produce inconsistent results because metaField order may vary
between servers and applications. To optimize queries on time-series
metaFields, query timeseries metaFields on scalar sub-fields rather than
entire metaFields.

The following example creates a time series collection:

.. code-block:: javascript

db.weather.insertMany( [
{
"metaField": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate( "2021-05-18T00:00:00.000Z" ),
"temp": 12
},
{
"metaField": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate( "2021-05-18T04:00:00.000Z" ),
"temp": 11
}
] )

The following query on the ``sensorId`` and ``type`` scalar sub-fields
returns the first document that matches the query criteria:

.. code-block:: javascript

db.weather.findOne( {
"metaField.sensorId": 5578,
"metaField.type": "temperature"
} )

Example output:

.. code-block:: javascript
:copyable: false

{
_id: ObjectId("6572371964eb5ad43054d572"),
metaField: { sensorId: 5578, type: 'temperature' },
timestamp: ISODate( "2021-05-18T00:00:00.000Z" ),
temp: 12
}
3 changes: 3 additions & 0 deletions source/core/timeseries/timeseries-procedures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ Example output:
_id: ObjectId("62f11bbf1e52f124b84479ad")
}

For more information on time series queries, see
:ref:`tsc-best-practice-optimize-query-performance`.

Run Aggregations on a Time Series Collection
--------------------------------------------

Expand Down

0 comments on commit e1d2806

Please sign in to comment.