|
| 1 | +======================= |
| 2 | +Time Series Collections |
| 3 | +======================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn about **time series collections** in |
| 17 | +MongoDB, and how to interact with them in the MongoDB Java driver. |
| 18 | + |
| 19 | +Time series collections efficiently store sequences of measurements over |
| 20 | +a period of time. Time series data consists of any data collected over |
| 21 | +time, metadata that describes the measurement, and the time of the |
| 22 | +measurement. |
| 23 | + |
| 24 | +.. list-table:: |
| 25 | + :widths: 33, 33, 33 |
| 26 | + :header-rows: 1 |
| 27 | + :stub-columns: 1 |
| 28 | + |
| 29 | + * - Example |
| 30 | + - Measurement |
| 31 | + - Metadata |
| 32 | + |
| 33 | + * - Sales Data |
| 34 | + - Revenue |
| 35 | + - Company |
| 36 | + |
| 37 | + * - Infection Rates |
| 38 | + - Amount of People Infected |
| 39 | + - Location |
| 40 | + |
| 41 | +Create a Time Series Collection |
| 42 | +------------------------------- |
| 43 | + |
| 44 | +To create a time series collection, pass the following parameters to the |
| 45 | +`createCollection() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoDatabase.html#createCollection(java.lang.String,com.mongodb.client.model.CreateCollectionOptions)>`__ |
| 46 | +method: |
| 47 | + |
| 48 | +- The name of the new collection to create |
| 49 | +- The `TimeSeriesOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/TimeSeriesOptions.html>`__ for creating the collection in a `CreateCollectionOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/CreateCollectionOptions.html>`__ object |
| 50 | + |
| 51 | +.. literalinclude:: /includes/fundamentals/code-snippets/TimeSeries.java |
| 52 | + :start-after: begin time series |
| 53 | + :end-before: end time series |
| 54 | + :emphasize-lines: 5 |
| 55 | + :language: java |
| 56 | + :dedent: |
| 57 | + |
| 58 | +.. important:: |
| 59 | + |
| 60 | + Versions prior to MongoDB 5.0 cannot create a time series collection. |
| 61 | + |
| 62 | +To check if you successfully created the collection, send the |
| 63 | +``"listCollections"`` command to the `runCommand() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoDatabase.html#runCommand(org.bson.conversions.Bson)>`__ method. |
| 64 | + |
| 65 | +.. literalinclude:: /includes/fundamentals/code-snippets/TimeSeries.java |
| 66 | + :start-after: begin check collection type |
| 67 | + :end-before: end check collection type |
| 68 | + :emphasize-lines: 1 |
| 69 | + :language: java |
| 70 | + :dedent: |
| 71 | + |
| 72 | +Your output should look similar to the following: |
| 73 | + |
| 74 | +.. code-block:: json |
| 75 | + :emphasize-lines: 7, 10 |
| 76 | + |
| 77 | + { |
| 78 | + "id": <some number>, |
| 79 | + "ns": "<db name>.$cmd.listCollections", |
| 80 | + "firstBatch": [ |
| 81 | + { |
| 82 | + "name": "<time series collection name>", |
| 83 | + "type": "timeseries", |
| 84 | + "options": { |
| 85 | + "expireAfterSeconds": <some number>, |
| 86 | + "timeseries": { ... } |
| 87 | + }, |
| 88 | + ... |
| 89 | + }, |
| 90 | + ... |
| 91 | + ] |
| 92 | + } |
| 93 | + |
| 94 | +Query a Time Series Collection |
| 95 | +------------------------------ |
| 96 | + |
| 97 | +To query in a time series collection, use the same conventions as you |
| 98 | +would for :doc:`retrieving </fundamentals/crud/read-operations/retrieve>` |
| 99 | +and :ref:`aggregating data <java-aggregation>`. |
| 100 | + |
| 101 | +.. note:: Window Functions |
| 102 | + |
| 103 | + MongoDB version 5.0 introduces window functions into the aggregation |
| 104 | + framework. You can use window functions to perform operations on a |
| 105 | + contiguous span of time series data. For more information, see our |
| 106 | + :ref:`Aggregates Builders guide <builders-aggregates-setWindowFields>`. |
| 107 | + |
0 commit comments