MongoDB Time Series
Overview
The data points sequence is referred to as time series. MongoDB Time series collection allows efficient storage of time series data. The behavior of time series collection is very similar to the normal collection behavior.
What is Time Series?
The data points sequence is referred to as time series. And in time series, insights are gained by analyzing changes over time. There are three components in the time series.
- Time: Time of recording data point
- Metadata(sometimes also known as source), metadata is just like a tag or label, which is used for unique identification of the series and rarely changes.
- Measurements(sometimes it is also known as values or metrics), measurements refer to the tracked data points with time increments. These are generally in the form of pair of key-value and, over time, they will change.
Time Series in MongoDB
MongoDB Time series collection allows efficient storage of time series data. In the MongoDB time series collections, writes organization is done in such a form that the storage of the same source data is alongside the other data points stored from the same point in time.
Benefits
In comparison to storing time series data in the normal collection, query efficiency is improved by storing the time series data in time series collection. And disk storage usage for the time series data is also reduced by it. The underlying columnar storage format is used by the time series collections for storing time series data. And it allows data storage in the time order with the clustered index(which is created automatically). The following benefits are provided by the columnar storage format of the time series collections.
- Complexity for working with time series data is reduced
- Query efficiency is improved
- Disk usage is reduced
- Read operations require less IO
Behavior
The behavior of the MongoDB time series collection is very similar to the normal MongoDB collection. Time series allows data insertion and querying just like normal collection insertion and querying. Time series is treated as a writable non-materialized view by MongoDB. At the time of data insertion, there is the automatic organization of the time series by the internal collection of the data. At the time of querying the data of the MongoDB time series collections, you are operating on a single document per measurement. The benefit of an optimized internal storage format is taken by the query performed in data in time series collections by enabling the result to be returned quickly.
Features of MongoDB Time Series
- Cost and complexity are reduced.
- Full-time series data lifecycle management becomes easier.
- Allows efficient storage of time series data.
- App development is simplified.
MongoDB Time Series Data
Time series refers to those data measurements which are taken at intervals of time. Sometimes the frequency of time series data coming to your database is very high, like data from stock markets, smart meter readings, or financial transactions data. And also, in some cases, every measurement will come to the database in very few time intervals. However, if you talk about time series data, it is not all about the frequency, but the main thing is the time presence. So by using MongoDB for the time-series data storage and working, it does not matter whether data is coming to the database every second, every minute, every 5 minutes, or every hour.
Examples
From the very start, MongoDB has been used by developers for storing time series data. MongoDB is considered a very efficient engine for time series data storage and processing, but it is required to know how to model it so that we can achieve a performant solution perfectly, but it could not be as straightforward. The new type of collection, i.e., time-series collections, is available from MongoDB 5.0. And this collection is designed particularly for working and storing the time-series data without any hindrance, and we need not worry about the optimization of the low-level model.
MongoDB Time Series Collections
MongoDB Time series collections are the new type of collection that was started in MongoDB 5.0. And the feel and look of these collections are similar to the normal collections of MongoDB. Reading, writing, and querying can be performed similarly to what we can perform in the regular MongoDB collections. And apart from that, time series collections also allow you to create secondary indexes by using the createIndex command. But internally it is natively optimized and supported for time series data storage and working.
Why Use MongoDB Time Series Collections?
MongoDB time series collections can be used as you want to work with the time series data, right?
Yes, it may be true sometimes, but apart from that there are so many other reasons for using the time series collections for time series data storage instead of using normal MongoDB collections for time series data.
At the time of the time series collections creation, there were many goals like efficient storage, performance, and ease of use. Time series collections enable you to perform operations with your data model as any other collection as a single document with a huge data structure and data types.
Modeling of time series data in such a way that is useful in the future also is eliminated by it. MongoDB Time series collections automatically take care of all these.
Your documents model can be designed more intuitively, just like any other MongoDB collection.
Storage schema for the storage, retrieval, and ingestion is optimized by the database by providing native techniques of compression, which enables efficient time series data storage without any worry of redundant fields along with the measurement data.
Creating and Querying MongoDB Time Series Collections
Before data insertion in the MongoDB time series collection, explicit collection creation is required by using the create command or db.createCollection() method:
Create a time series collection:
Here we are making a collection with the name weather and it has a timeseries object. The "timeseries" feature has "timeField", "metaField", and "granularity" fields.
"timeField" is a required field that will store the data for every time series document.
"metaField" is an optional field that holds any additional metadata associated with every time series document.
"granularity" is an optional field and it allows to store values in seconds, minutes, or hours.
Inserting Measurements
We can insert a single document by the command given below:
Every document you desire to insert must have one measurement. For multiple documents insertion simultaneously, write the command given below:
Querying
Time series collection can be queried the same as the querying of the standard MongoDB Collection.
Run the following command for returning a single document from the MongoDB time series collection:
And it will return the below document as an output
Output:
Running Aggregations
The aggregation pipeline can be used to achieve additional functionality in querying.
The aggregation pipeline given in the example performs document grouping by the measurement date, and the average of all the measurements of that day will be returned by it.
Output:
FAQs
Q: Among MongoDB time series collection and normal MongoDB collection, which is more preferred for the time series data ?
A: MongoDB Time series collection is preferred for storing the time series data, as in comparison to storing time series data in the normal collection, query efficiency is improved by storing the time series data in time series collection. And disk storage usage for the time series data is also reduced by it.
Conclusion
- Data points sequence is referred to as time series.
- MongoDB Time series collection allows efficient storage of time series data.
- In comparison to storing time series data in the normal collection, query efficiency is improved by storing the time series data in time series collection. And disk storage usage for the time series data is also reduced by it.
- Behavior of MongoDB time series collection is very similar to the normal MongoDB collection behavior.
- Time series refers to those data measurements which are taken at intervals of time.
- Time-series collections are available from mongoDB 5.0.
- There are so many other reasons for using the MongoDB time series collections for time series data storage instead of using normal MongoDB collections for time series data.