-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45183-time-series-collections #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gmiller-mdb
merged 18 commits into
mongodb:standardization
from
gmiller-mdb:DOCSP-45183-time-series-collections
Jan 17, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d5c1f7d
DOCSP-45183-time-series-collections
gmiller-mdb e5c20d4
style guide
gmiller-mdb 1a33a24
added more copy
gmiller-mdb 3b47405
added page
gmiller-mdb 88abab7
formatting
gmiller-mdb abb05b6
trying for output
gmiller-mdb 718a9f9
again
gmiller-mdb dad3ee1
formatting
gmiller-mdb 6be35cd
add
gmiller-mdb 0943e4b
feedback
gmiller-mdb 54cb8a7
ahh
gmiller-mdb 9bd98fd
toctree
gmiller-mdb 7ba6749
api links
gmiller-mdb 251324b
feedback
gmiller-mdb 25751dc
keyword
gmiller-mdb 227a043
keyword 2
gmiller-mdb 050267d
feedback
gmiller-mdb 548a9e9
fix typo
gmiller-mdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
.. _ruby-data-formats: | ||
|
||
============ | ||
Data Formats | ||
============ | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:description: Learn how to use indexes by using the MongoDB Ruby Driver. | ||
:keywords: ruby, query, collections, time series | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
:maxdepth: 1 | ||
|
||
Time Series Data </data-formats/time-series> | ||
|
||
Overview | ||
-------- | ||
|
||
You can use several types of specialized data formats in your {+driver-short+} | ||
application. To learn how to work with these data formats, see the following | ||
sections: | ||
|
||
- :ref:`ruby-time-series`: Learn how to create a time series collection and interact with time series data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
.. _ruby-time-series: | ||
|
||
================ | ||
Time Series Data | ||
================ | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: ruby, time series, collections, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use {+driver-short+} to store | ||
and interact with **time series data**. | ||
|
||
Time series data is composed of the following components: | ||
|
||
- Measured quantity | ||
- Timestamp for the measurement | ||
- Metadata that describes the measurement | ||
|
||
The following table describes sample situations for which you could store time | ||
series data: | ||
|
||
.. list-table:: | ||
:widths: 33, 33, 33 | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Situation | ||
- Measured Quantity | ||
- Metadata | ||
|
||
* - Recording monthly sales by industry | ||
- Revenue in USD | ||
- Company, country | ||
|
||
* - Tracking weather changes | ||
- Precipitation level | ||
- Location, sensor type | ||
|
||
* - Recording fluctuations in housing prices | ||
- Monthly rent price | ||
- Location, currency | ||
|
||
.. _ruby-time-series-create: | ||
|
||
Create a Time Series Collection | ||
------------------------------- | ||
|
||
.. important:: Server Version for Time Series Collections | ||
|
||
To create and interact with time series collections, you must be | ||
connected to a deployment running {+mdb-server+} 5.0 or later. | ||
|
||
To create a time series collection, you must pass an options hash that contains | ||
the specifications for the collection. You can specify the following specifications | ||
for your time series collection: | ||
|
||
- ``:timeField``: Specifies the field that stores a timestamp in each time series | ||
document. | ||
- ``:metaField``: Specifies the field that stores metadata in each time series | ||
document. | ||
- ``:granularity``: Specifies the approximate time between consecutive timestamps. | ||
The possible values are ``'seconds'``, ``'minutes'``, and ``'hours'``. | ||
- ``:bucketMaxSpanSeconds``: Sets the maximum time between timestamps in the | ||
same bucket. | ||
- ``:bucketRoundingSeconds``: Sets the number of seconds to round down by when | ||
MongoDB sets the minimum timestamp for a new bucket. Must be equal to | ||
``:bucketMaxSpanSeconds``. | ||
|
||
See :manual:`Command Fields </reference/command/create/#command-fields>` | ||
in the {+mdb-server+} manual entry on the ``create`` command to learn more about | ||
these parameters. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
The following example uses the ``Collection#create`` method to create a time series | ||
collection named ``october2024`` with the ``:timeField``` option set to ``"timestamp"``: | ||
|
||
.. literalinclude:: /includes/usage-examples/time-series.rb | ||
:language: ruby | ||
:dedent: | ||
:start-after: start-create | ||
:end-before: end-create | ||
|
||
To verify that you have successfully created the collection, print a list of all | ||
collections in your database and filter by collection name, as shown in the following | ||
code: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: /includes/usage-examples/time-series.rb | ||
:language: ruby | ||
:start-after: start-correct | ||
:end-before: end-correct | ||
:dedent: | ||
|
||
.. output:: | ||
:language: json | ||
:visible: false | ||
|
||
[ | ||
{ | ||
"name": "october2024", | ||
"type": "timeseries", | ||
"options": { | ||
"timeseries": { | ||
"timeField": "timestamp", | ||
"granularity": "seconds", | ||
"bucketMaxSpanSeconds": 3600 | ||
} | ||
}, | ||
"info": { | ||
"readOnly": false | ||
} | ||
} | ||
] | ||
|
||
|
||
.. _ruby-time-series-write: | ||
|
||
Store Time Series Data | ||
---------------------- | ||
|
||
You can insert data into a time series collection by using the ``insert_one`` | ||
or ``insert_many`` method and specifying the measurement, timestamp, and | ||
metadata in each inserted document. | ||
|
||
To learn more about inserting documents, see the :ref:`ruby-write-insert` guide. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
This example inserts New York City temperature data into the ``october2024`` | ||
time series collection created in the preceding :ref:`ruby-time-series-create` | ||
section. Each document contains the following fields: | ||
|
||
- ``temperature``, which stores temperature measurements in degrees Fahrenheit | ||
- ``location``, which stores location metadata | ||
- ``timestamp``, which stores the measurement timestamp | ||
|
||
.. literalinclude:: /includes/usage-examples/time-series.rb | ||
:language: ruby | ||
:dedent: | ||
:start-after: start-insert | ||
:end-before: end-insert | ||
|
||
.. TODO: add link | ||
|
||
.. .. tip:: Formatting Dates and Times | ||
|
||
.. To learn more about using ``datetime`` objects in {+driver-short+}, see | ||
.. :ref:`ruby-dates-times`. | ||
|
||
.. _ruby-time-series-read: | ||
|
||
Query Time Series Data | ||
---------------------- | ||
|
||
You can use the same syntax and conventions to query data stored in a time | ||
series collection as you use when performing read or aggregation operations on | ||
other collections. | ||
|
||
.. TODO: add links | ||
.. To learn more about these operations, see :ref:`ruby-read` | ||
.. and :ref:`ruby-aggregation`. | ||
|
||
.. _ruby-time-series-addtl-info: | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about the concepts in this guide, see the following {+mdb-server+} | ||
manual entries: | ||
|
||
- :manual:`Time Series </core/timeseries-collections/>` | ||
- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` | ||
- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about the methods mentioned in this guide, see the following | ||
API documentation: | ||
|
||
- `create <{+api-root+}/Mongo/Collection.html#create-instance_method>`__ | ||
- `list_collections <{+api-root+}/Mongo/Database.html#list_collections-instance_method>`__ | ||
- `insert_one <{+api-root+}/Mongo/Collection.html#insert_one-instance_method>`__ | ||
- `insert_many <{+api-root+}/Mongo/Collection.html#insert_many-instance_method>`__ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'bundler/inline' | ||
gemfile do | ||
source 'https://rubygems.org' | ||
gem 'mongo' | ||
end | ||
|
||
|
||
# start-create | ||
client = Mongo::Client.new('<connection string>', database: 'weather') | ||
collection_name = 'october2024' | ||
|
||
time_series_options = { timeField: 'timestamp' } | ||
database = client.database | ||
database.command( | ||
create: collection_name, | ||
timeseries: time_series_options | ||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: indent these params appropriately. I believe the ruby convention is 3 spaces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was told earlier it was 2, so I updated it to 2! |
||
) | ||
# end-create | ||
|
||
# start-correct | ||
collections = database.list_collections(filter: { name: 'october2024' }).to_a | ||
puts collections | ||
# end-correct | ||
|
||
# start-insert | ||
client = Mongo::Client.new('<connection string>', database => 'your_db') | ||
collection = client[:october2024] | ||
|
||
document_list = [ | ||
{ temperature: 77, location: "New York City", timestamp: DateTime.new(2024, 10, 22, 6, 0, 0) }, | ||
{ temperature: 74, location: "New York City", timestamp: DateTime.new(2024, 10, 23, 6, 0, 0) } | ||
] | ||
|
||
collection.insert_many(document_list) | ||
#end-insert |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.