Skip to content

Commit 8d31b61

Browse files
DOCSP-17732 time series page (#154)
* added time series page
1 parent 12a4d0b commit 8d31b61

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fundamentals
2323
/fundamentals/monitoring
2424
/fundamentals/gridfs
2525
/fundamentals/csfle
26+
/fundamentals/time-series
2627

2728
The Fundamentals section contains detailed information to help you
2829
expand your knowledge on how to use MongoDB with the Java driver. We

source/fundamentals/builders/aggregates.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ The following example creates a pipeline stage that executes two parallel aggreg
538538
:language: java
539539
:dedent:
540540

541+
.. _builders-aggregates-setWindowFields:
542+
541543
SetWindowFields
542544
---------------
543545

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public package docs;
2+
3+
import org.bson.BsonInt64;
4+
import org.bson.Document;
5+
6+
import com.mongodb.client.MongoClient;
7+
import com.mongodb.client.MongoClients;
8+
import com.mongodb.client.MongoDatabase;
9+
import com.mongodb.MongoException;
10+
import com.mongodb.client.model.CreateCollectionOptions;
11+
import com.mongodb.client.model.TimeSeriesOptions;
12+
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
public class TimeSeriesCollection {
17+
18+
public static void main(String[] args) {
19+
20+
final String uri = System.getenv("DRIVER_REF_URI");
21+
22+
try(MongoClient mongoClient = MongoClients.create(uri)){
23+
24+
// begin time series
25+
MongoDatabase database = mongoClient.getDatabase("fall_weather");
26+
TimeSeriesOptions tsOptions = new TimeSeriesOptions("temperature");
27+
CreateCollectionOptions collOptions = new CreateCollectionOptions().timeSeriesOptions(tsOptions);
28+
29+
database.createCollection("september2021", collOptions);
30+
// end time series
31+
try {
32+
// begin check collection type
33+
Document commandResult = database.runCommand(new Document("listCollections", new BsonInt64(1)));
34+
35+
List<String> keys = Arrays.asList("cursor");
36+
System.out.println("listCollections: " + commandResult.getEmbedded(keys, Document.class).toJson());
37+
// end check collection type
38+
} catch (MongoException me) {
39+
System.err.println("An error occurred: " + me);
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)