@@ -24,10 +24,19 @@ Set Granularity for Time Series Data
24
24
<5.0-known-issue-granularity>`.
25
25
26
26
When you create a :ref:`time series collection
27
- <manual-timeseries-collection>`, set the granularity to the value that
28
- is the closest match to the time span between consecutive incoming
29
- measurements that have the same unique value for the ``metaField``
30
- field:
27
+ <manual-timeseries-collection>`, you can specify the
28
+ ``granularity`` parameter. The ``granularity`` determines how data
29
+ in the time series collection is stored internally. For details about
30
+ how MongoDB stores time series data, see :ref:`flexible-bucketing`.
31
+
32
+ By default, ``granularity`` is set to ``"seconds"``. To optimize
33
+ data storage, set the ``granularity`` to a value that is closest
34
+ to the ingestion rate for your data source as specified by
35
+ the ``metaField`` field. The ingestion rate is the time span between
36
+ consecutive incoming measurements that have the same unique value for
37
+ the ``metaField``.
38
+
39
+ Consider the following example:
31
40
32
41
.. code-block:: javascript
33
42
@@ -43,19 +52,12 @@ field:
43
52
}
44
53
)
45
54
46
- Setting the ``granularity`` parameter accurately improves performance by
47
- optimizing how data in the time series collection is stored internally.
48
-
49
- To set the parameter accurately, choose a ``granularity`` value that is
50
- closest to the ingestion rate for a unique data source as specified by
51
- the value for the ``metaField`` field.
52
-
53
- For example, if your ``metaField`` data identifies weather sensors and
55
+ If your ``metaField`` data identifies weather sensors and
54
56
you ingest data from each individual sensor once every 5 minutes, you
55
57
should choose ``"minutes"``. Even if you have thousands of sensors and
56
58
the data coming in from different sensors is only seconds apart, the
57
59
``granularity`` should still be based on the ingestion rate for one
58
- sensor that is uniquely identified by its metadata.
60
+ sensor that is uniquely identified by its metadata.
59
61
60
62
In the following table, you can see the max time span of data that is
61
63
stored together for each ``granularity`` value:
@@ -148,3 +150,57 @@ a time. From ``"seconds"`` to ``"minutes"`` or from ``"minutes"`` to
148
150
149
151
You cannot modify the ``granularity`` of a sharded time series
150
152
collection.
153
+
154
+ .. _flexible-bucketing:
155
+
156
+ Flexible Bucketing
157
+ ~~~~~~~~~~~~~~~~~~
158
+
159
+ MongoDB groups incoming time series data into buckets. By setting the
160
+ ``granularity`` parameter, you control how frequently data is bucketed
161
+ based on the ingestion rate of your data.
162
+
163
+ Starting in MongoDB 6.3, you can specify the bucket boundaries to
164
+ more accurately control how time series data is bucketed. Use the
165
+ following parameters:
166
+
167
+ - ``bucketMaxSpanSeconds``, the maximum time span between measurements
168
+ in a bucket.
169
+ - ``bucketRoundingSeconds``, the time interval that determines the
170
+ starting timestamp for a new bucket.
171
+
172
+ .. note::
173
+
174
+ If you want to specify the bucket boundaries:
175
+
176
+ - You must set both ``bucketMaxSpanSeconds`` and ``bucketRoundingSeconds``.
177
+ - Both parameters must have the same value.
178
+ - You can't additionally set the ``granularity`` parameter.
179
+
180
+ Consider the following time series collection:
181
+
182
+ .. code-block:: javascript
183
+
184
+ db. createCollection(
185
+ "weather24h",
186
+ {
187
+ timeseries: {
188
+ timeField: "timestamp",
189
+ metaField: "metadata",
190
+ bucketMaxSpanSeconds: 60,
191
+ bucketRoundingSeconds: 60
192
+ }
193
+ }
194
+ )
195
+
196
+ Each bucket has a maximum time span of 60 seconds. When MongoDB opens a
197
+ new bucket, the starting timestamp is rounded down to the nearest 60-second
198
+ interval.
199
+
200
+ For example, if you insert a new measurement into the collection with a
201
+ timestamp of ``11:59:59``, the measurement is added to the bucket
202
+ with boundaries between ``11:59:00`` and ``12:00:00`` (non-inclusive).
203
+ If the bucket doesn't exist, the starting timestamp of the new bucket
204
+ is determined by rounding ``11:59:59`` down to ``11:59:00``.
205
+
206
+ For more information on time series parameters, see :ref:`time-series-fields`.
0 commit comments