Skip to content

Commit e01c706

Browse files
mongodbenrustagir
andauthored
(DOCSP-29231): Kotlin timeseries page (#55)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-29231 Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-29231/fundamentals/time-series/ ## Self-Review Checklist - [ ] Is this free of any warnings or errors in the RST? - [ ] Did you run a spell-check? - [ ] Did you run a grammar-check? - [ ] Are all the links working? --------- Co-authored-by: Rea Rustagi <[email protected]>
1 parent a29f0ab commit e01c706

File tree

6 files changed

+104
-42
lines changed

6 files changed

+104
-42
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
import com.mongodb.*
3+
import com.mongodb.client.model.CreateCollectionOptions
4+
import com.mongodb.client.model.Filters.*
5+
import com.mongodb.client.model.Projections.*
6+
import com.mongodb.client.model.TimeSeriesOptions
7+
import com.mongodb.kotlin.client.coroutine.MongoClient
8+
import io.github.cdimascio.dotenv.dotenv
9+
import kotlinx.coroutines.flow.toList
10+
import kotlinx.coroutines.runBlocking
11+
import org.bson.json.JsonWriterSettings
12+
import org.junit.jupiter.api.AfterAll
13+
import org.junit.jupiter.api.Assertions.*
14+
import org.junit.jupiter.api.TestInstance
15+
import java.util.*
16+
import kotlin.test.*
17+
18+
19+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
20+
internal class TimeSeriesTest {
21+
22+
companion object {
23+
val dotenv = dotenv()
24+
val mongoClient = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
25+
26+
@AfterAll
27+
@JvmStatic
28+
fun afterAll() {
29+
runBlocking {
30+
mongoClient.close()
31+
}
32+
}
33+
}
34+
35+
@Test
36+
fun timeSeriesTest() = runBlocking {
37+
// :snippet-start: create-time-series-collection
38+
val database = mongoClient.getDatabase("fall_weather")
39+
val tsOptions = TimeSeriesOptions("temperature")
40+
val collOptions = CreateCollectionOptions().timeSeriesOptions(tsOptions)
41+
42+
database.createCollection("september2021", collOptions)
43+
// :snippet-end:
44+
// :snippet-start: check-time-series-collection-created
45+
val commandResult = database.listCollections().toList()
46+
.find { it["name"] == "september2021" }
47+
48+
println(commandResult?.toJson(JsonWriterSettings.builder().indent(true).build()))
49+
// :snippet-end:
50+
assertEquals("timeseries", commandResult?.getString("type") )
51+
52+
// clean up
53+
database.drop()
54+
}
55+
56+
57+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
val commandResult = database.listCollections().toList()
2+
.find { it["name"] == "september2021" }
3+
4+
println(commandResult?.toJson(JsonWriterSettings.builder().indent(true).build()))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val database = mongoClient.getDatabase("fall_weather")
2+
val tsOptions = TimeSeriesOptions("temperature")
3+
val collOptions = CreateCollectionOptions().timeSeriesOptions(tsOptions)
4+
5+
database.createCollection("september2021", collOptions)

source/fundamentals.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ Fundamentals
1919
/fundamentals/indexes
2020
/fundamentals/logging
2121
/fundamentals/monitoring
22+
/fundamentals/time-series
2223
/fundamentals/encrypt-fields
2324

2425
.. TODO : add back in after MVP
2526
.. /fundamentals/auth
2627
.. /fundamentals/enterprise-auth
2728
.. /fundamentals/collations
2829
.. /fundamentals/gridfs
29-
.. /fundamentals/time-series
30+
.. /fundamentals/csfle
3031

3132
.. include:: /includes/fundamentals-sections.rst

source/fundamentals/time-series.draft renamed to source/fundamentals/time-series.txt

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Overview
1414
--------
1515

1616
In this guide, you can learn about **time series collections** in
17-
MongoDB, and how to interact with them in the MongoDB Java driver.
17+
MongoDB, and how to interact with them in the MongoDB Kotlin driver.
1818

1919
Time series collections efficiently store sequences of measurements over
2020
a period of time. Time series data consists of any data collected over
@@ -42,54 +42,46 @@ Create a Time Series Collection
4242
-------------------------------
4343

4444
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)>`__
45+
`createCollection() <{+api-kotlin+}/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-database/create-collection.html>`__
4646
method:
4747

4848
- 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
49+
- The `TimeSeriesOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/TimeSeriesOptions.html>`__
50+
for creating the collection in a `CreateCollectionOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/CreateCollectionOptions.html>`__ object
5051

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:
52+
.. literalinclude:: /examples/generated/TimeSeriesTest.snippet.create-time-series-collection.kt
53+
:language: kotlin
5754

5855
.. important::
5956

6057
Versions prior to MongoDB 5.0 cannot create a time series collection.
6158

6259
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": { ... }
60+
``"listCollections"`` command to the `runCommand() <{+api-kotlin+}/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-database/run-command.html>`__ method.
61+
62+
.. io-code-block::
63+
64+
.. input:: /examples/generated/TimeSeriesTest.snippet.check-time-series-collection-created.kt
65+
:language: kotlin
66+
67+
.. output::
68+
:language: json
69+
:emphasize-lines: 3, 5-9
70+
71+
{
72+
"name": "september2021",
73+
"type": "timeseries",
74+
"options": {
75+
"timeseries": {
76+
"timeField": "temperature",
77+
"granularity": "seconds",
78+
"bucketMaxSpanSeconds": 3600
79+
}
8780
},
88-
...
89-
},
90-
...
91-
]
92-
}
81+
"info": {
82+
"readOnly": false
83+
}
84+
}
9385

9486
Query a Time Series Collection
9587
------------------------------
@@ -102,5 +94,8 @@ and :ref:`aggregating data <kotlin-aggregation>`.
10294

10395
MongoDB version 5.0 introduces window functions into the aggregation
10496
pipeline. 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>`.
97+
contiguous span of time series data.
98+
99+
.. TODO:(DOCSP-29203): add back after Aggregates Builders page added back
100+
.. For more information, see our
101+
.. :ref:`Aggregates Builders guide <builders-aggregates-setWindowFields>`.

source/includes/fundamentals-sections.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Fundamentals section:
1111
- :doc:`Create Indexes to Speed Up Queries </fundamentals/indexes>`
1212
- :doc:`Log Events in the Driver </fundamentals/logging>`
1313
- :doc:`Monitor Driver Events </fundamentals/monitoring>`
14+
- :doc:`Use a Time Series Collection </fundamentals/time-series>`
1415
- :doc:`Encrypt Fields in a Document </fundamentals/encrypt-fields>`
1516

1617
.. TODO : add back in after MVP
1718
.. - :doc:`Authenticate with MongoDB </fundamentals/auth>`
1819
.. - :doc:`Transform your Data </fundamentals/aggregation>`
1920
.. - :doc:`Sort Using Collations </fundamentals/collations>`
2021
.. - :doc:`Store and Retrieve Large Files in MongoDB </fundamentals/gridfs>`
21-
.. - :doc:`Use a Time Series Collection </fundamentals/time-series>`

0 commit comments

Comments
 (0)