{"id":104932,"date":"2025-01-27T14:45:26","date_gmt":"2025-01-27T14:45:26","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=104932"},"modified":"2025-01-27T15:41:18","modified_gmt":"2025-01-27T15:41:18","slug":"creating-time-series-collections-in-mongodb","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/nosql\/mongodb\/creating-time-series-collections-in-mongodb\/","title":{"rendered":"Creating Time Series Collections in MongoDB"},"content":{"rendered":"<p><strong>This article is part of Robert Sheldon's continuing series on Mongo DB. To see all of the items in the series, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/collections\/robert-sheldon-ongoing-mongodb-primer\/\">click here<\/a>.<\/strong><\/p>\n\n\n\n\n<p>Throughout this series, I\u2019ve introduced you to different features in MongoDB and provided examples to help demonstrate how the database system works. The examples have all been based on conventional collections, the type that MongoDB creates by default. However, MongoDB also supports other types of collections, including the time series collection, which can benefit many of today\u2019s event-driven workloads.<\/p>\n\n\n\n<p>The documents in a time series collection represent a sequence of data points, with each document recording an event at a specific point of time. For example, a machine sensor might generate temperature readings that are transformed into individual documents and stored in a time series collection.<\/p>\n\n\n\n<p>A time series collection is optimized to handle these types of documents and the workloads they support, offering improved query performance and reduced storage consumption. MongoDB automatically stores the collection\u2019s data in groups of related documents and indexes them based on their date values and unique group identifiers.<\/p>\n\n\n\n<p>In this article, I introduce you to the time series collection and demonstrate different ways you can create them in MongoDB Shell. If you want to try out the examples, you can use the version of Shell embedded in MongoDB Compass or the one you access through your system\u2019s command-line interface. You can also create time series collections in the Compass GUI, although this article focuses on the Shell commands.<\/p>\n\n\n\n<p>Note: For the examples in this article, I used the same MongoDB Atlas environment I used for the previous articles in this series. Refer to the first article for details about setting up these environments. For this article, the examples are based on the <code>iot<\/code> database, which you can create in advance or when you try out the exercises.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-a-time-series-collection-to-a-mongodb-database\">Adding a time series collection to a MongoDB database<\/h2>\n\n\n\n<p>To create a time series collection in MongoDB Shell, you can use the <code>createCollection<\/code> method, just like you can for a conventional collection. The primary difference is that, for a time series collection, you must include the <code>timeseries<\/code> option in your collection definition, as shown in the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.createCollection(\n  \"collection_name\",\n  {\n    timeseries: \n    {\n      timeField: \"field_name\",\n      metaField: \"field_name\",\n      granularity_options\n    },\n    expireAfterSeconds: num_seconds\n  }\n);<\/pre>\n\n\n\n<p>The command\u2019s syntax consists of the following elements:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><strong>db.<\/strong> System variable for referencing the current database and accessing the properties and methods available to the database object. For this article, you should ensure that <code>iot<\/code> is the current database.<\/li>\n\n\n\n<li><strong>createCollection.<\/strong> Database method for creating a collection in the current database.<\/li>\n\n\n\n<li><strong><em>collection_name<\/em>.<\/strong> Placeholder for the name of the new collection. For this article, we\u2019ll be creating the <code>pressure<\/code> collection.<\/li>\n\n\n\n<li><strong>timeseries.<\/strong> An option available to the <code>createCollection<\/code> method for creating a time series collection. The option defines an embedded document that includes parameters specific to a time series collection.<\/li>\n\n\n\n<li><strong>timeField.<\/strong> A <code>timeseries<\/code> parameter that specifies a date field in the collection\u2019s documents. The field must be defined as a valid BSON data type. BSON is a binary encoding of JSON.<\/li>\n\n\n\n<li><strong>metaField.<\/strong> An optional <code>timeseries<\/code> parameter that specifies a metadata field in the collection\u2019s documents. The field should contain data that can uniquely identify a related group of documents. For example, the field might identify a weather sensor and its location. Only documents that are generated by the same sensor at the same location can be included in the same bucket. The field value should rarely, if ever, change. Although this setting is optional, its inclusion can improve query performance because it can be used as part of a compound index along with the field assigned to the <code>timeField<\/code> parameter.<\/li>\n\n\n\n<li><strong><em>granularity_options<\/em>.<\/strong> Placeholder for one or more parameters that specify the collection\u2019s granularity, which determines how the collection\u2019s documents are bucketed into related groups of data. I discuss the granularity options in more detail later in the article.<\/li>\n\n\n\n<li><strong>expireAfterSeconds.<\/strong> An optional parameter that lets you specify whether the documents in a time series collection should be automatically deleted after a certain amount of time. If the setting is included, it should be defined with an integer value that indicates when the documents will be deleted. The integer, as indicated by the <code>num_seconds<\/code> placeholder, determines the number of seconds that should pass before a document expires.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>The documents in a time series collection typically contain a date field that is assigned to the <code>timeField<\/code> parameter, a metadata field that is assigned to the <code>metaField<\/code> parameter, and some type of measure specific to the date field and metadata field. For instance, the documents in a time series collection that tracks global temperatures might include the following three fields:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li>A date field that records when the temperature was measured.<\/li>\n\n\n\n<li>A metadata field that identifies the weather sensor and its location.<\/li>\n\n\n\n<li>A measure field that records the temperature.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>Each document in a time series collection represents an event at a specific point in time, such as the weather station\u2019s temperature readings. Other examples include website views, stock trades, inventory changes, sensor data from internet of things (IoT) devices, and a variety of other use cases. The key is to define your time series collections to meet the specific needs of your workloads. You\u2019ll get a better sense of how each of these elements work as we progress through the article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-time-series-collection\">Creating a time series collection<\/h2>\n\n\n\n<p>Now that we\u2019ve reviewed the syntax, let\u2019s look at an example of how to create a time series collection. We\u2019ll start with a basic collection that uses the default granularity and does not expire the documents.<\/p>\n\n\n\n<p>You\u2019ll be creating the collection in the <code>iot<\/code> database, so you\u2019ll need to change the context to that database. To do so, launch MongoDB Shell and, at the command prompt, enter the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use iot;<\/pre>\n\n\n\n<p>The command switches the shell\u2019s database context to <code>iot<\/code>. You can use this command even if you have not yet created the database. If the database does not exist, MongoDB will automatically create it when you add the collection.<\/p>\n\n\n\n<p>Once you\u2019ve established the database context, you can use the <code>createCollection<\/code> method to add the <code>pressure<\/code> collection, as shown in the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.createCollection(\n  \"pressure\",\n  {\n    timeseries: {\n      timeField: \"timestamp\",\n      metaField: \"source\"\n    }\n  }\n);<\/pre>\n\n\n\n<p>The <code>timeseries<\/code> element in the collection definition includes the following two parameters:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li>The <code>timeField<\/code> parameter specifies the <code>timestamp<\/code> field, which contains the document\u2019s timestamp.<\/li>\n\n\n\n<li>The <code>metaField<\/code> parameter specifies the <code>source<\/code> field, which is an embedded document that contains a system identifier and sensor identifier.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>That\u2019s all there is to creating a basic time series collection. The trick is to know in advance which fields you plan to assign to the <code>timeField<\/code> and <code>metaField<\/code> parameters. The fields will be specific to the documents you\u2019ll be inserting into the collection.<\/p>\n\n\n\n<p>When you run the <code>createCollection<\/code> command, MongoDB automatically creates a compound index on the fields specified in the <code>timeField<\/code> and <code>metaField<\/code> parameters. In this case, MongoDB creates the index on the <code>source<\/code> and <code>timestamp<\/code> fields, as indicated by the index name, <code>source_1_timestamp_1<\/code>.<\/p>\n\n\n\n<p>After you create the collection, you can then run the following <code>insertMany<\/code> command to add sample data to the collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.pressure.insertMany([\n  {\n    \"timestamp\": ISODate(\"2024-12-01T12:05:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 122\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-01T12:35:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 137\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-01T16:05:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 129\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-01T16:35:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 133\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-01T20:05:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 87\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-01T20:35:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 113\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-02T12:05:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 121\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-02T12:35:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 129\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-02T16:05:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 127\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-02T16:35:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 131\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-02T20:05:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 139\n  },\n  {\n    \"timestamp\": ISODate(\"2024-12-02T20:35:00.000Z\"),\n    \"source\": { \"systemId\": 4937, \"sensorId\": 37217 },\n    \"reading\": 128\n  }\n]);<\/pre>\n\n\n\n<p>When you run this command, you should receive an acknowledgment message indicating that 12 documents were added to the collection. The message should also list each document\u2019s <code>_id<\/code> value. As with a conventional collection, MongoDB automatically adds an <code>_id<\/code> field to each document to ensure its uniqueness, unless you\u2019ve specifically included the field when inserting the documents.<\/p>\n\n\n\n<p>Each document in the <code>pressure<\/code> collection provides a record of a pressure reading from a sensor on a piece of machinery, equipment or other type of system, such as you might find in a manufacturing plant. The document includes a <code>timestamp<\/code> field, <code>source<\/code> field, and <code>reading<\/code> field:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li>The value for the <code>timestamp<\/code> field uses the <code>ISODate<\/code> function to convert the date\/time value from a string to a <code>Date<\/code> object.<\/li>\n\n\n\n<li>The value for the <code>source<\/code> field is an embedded document. The document specifies a system ID that uniquely identifies the piece of equipment or other system that is being monitored. The document also includes a unique ID for the sensor itself. In this way, the sensors on a single system can be tracked individually.<\/li>\n\n\n\n<li>Each document also includes the <code>reading<\/code> field, which provides the pressure reading at the time the pressure was measured.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>In this case, the sample documents are all from the same sensor on the same system. In a real-world setting, there would be many more records, with documents for different systems and sensors. The documents would be stored in multiple buckets, based on the values in their <code>timestamp<\/code> and <code>source<\/code> fields.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-querying-a-time-series-collection\">Querying a time series collection<\/h2>\n\n\n\n<p>You can query a time series collection just like you can a conventional collection. For example, the following command uses the <code>find<\/code> method to return only a subset of documents from the collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.pressure.find( { \"reading\": { $lt: 120 } } );<\/pre>\n\n\n\n<p>The command uses the <code>$lt<\/code> operator to specify that a document must have a <code>reading<\/code> value less than <code>120<\/code>. As a result, the command returns only two documents, which are shown in the following results:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n  timestamp: 2024-12-01T20:05:00.000Z,\n  source: {\n    sensorId: 37217,\n    systemId: 4937\n  },\n  reading: 87,\n  _id: ObjectId('674dfe47bd3a4413e2aebb01')\n}\n{\n  timestamp: 2024-12-01T20:35:00.000Z,\n  source: {\n    sensorId: 37217,\n    systemId: 4937\n  },\n  reading: 113,\n  _id: ObjectId('674dfe47bd3a4413e2aebb02')\n}<\/pre>\n\n\n\n<p>You can also create more complex queries, including aggregations. For instance, the following command uses the <code>aggregate<\/code> method to find the average <code>reading<\/code> value for each date in the collection\u2019s documents.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.pressure.aggregate([\n  { $match: { \"reading\": { $gt: 120 } } },\n  {\n    $project: {\n      date: {\n        $dateToParts: { \"date\": \"$timestamp\" }\n      },\n      \"reading\": 1\n    }\n  },\n  {\n    $group: {\n      _id: {\n        \"date\": {\n          \"year\": \"$date.year\",\n          \"month\": \"$date.month\",\n          \"day\": \"$date.day\"\n        }\n      },\n      \"avgReading\": { $avg: \"$reading\" }\n    }\n  },\n  { $project: {\n      _id: 0,\n      \"date\": {\n        $concat: [ \n          { $toString: \"$_id.date.year\" }, \"\/\",\n          { $toString: \"$_id.date.month\" }, \"\/\",\n          { $toString: \"$_id.date.day\" }\n       ]\n      },\n      \"avgReading\": { $round: [ \"$avgReading\", 2 ], } } \n  },\n  { $sort: { \"date\": 1 }}\n]);<\/pre>\n\n\n\n<p>The command\u2019s aggregation includes multiple stages. The first stage (<code>$match<\/code>) limits the documents to those with a <code>reading<\/code> value greater than <code>120<\/code>. The stages that follow then extract and parse the date from the <code>timestamp<\/code> value and group the documents by each date. Next, the aggregation finds the average pressure reading for each date. In this case, the documents include only two dates, so the results include only the following two averages:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n  date: '2024\/12\/1',\n  avgReading: 130.25\n}\n{\n  date: '2024\/12\/2',\n  avgReading: 129.17\n}<\/pre>\n\n\n\n<p>If this were a larger data set, there would likely be more dates, but those included here should be enough to demonstrate how the aggregation works with a time series collection. It also shows how you can interact with the collection just like a conventional collection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-configuring-the-granularity-on-a-time-series-collection\">Configuring the granularity on a time series collection<\/h2>\n\n\n\n<p>As I pointed out earlier, MongoDB groups the documents in a time series collection into multiple buckets. Documents within a bucket share the same <code>metaField<\/code> value. In addition, their <code>timeField<\/code> values fall within a defined range, such as occurring within the same hour or same day. The exact interval of time is determined by the collection\u2019s granularity.<\/p>\n\n\n\n<p>By default, the granularity is set to <code>seconds<\/code>, which means the documents within a particular bucket have a timestamp within the same hour. However, you can set the granularity at larger time spans.<\/p>\n\n\n\n<p>MongoDB provides two methods for configuring the granularity. The first is to use the <code>granularity<\/code> parameter, and the second is to use the set of custom parameters. Let\u2019s start with the <code>granularity<\/code> parameter, which takes one of the following three values:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><code><strong>seconds<\/strong><\/code><strong> (default).<\/strong> A bucket can contain up to one hour\u2019s worth of events.<\/li>\n\n\n\n<li><code><strong>minutes<\/strong><\/code><strong>.<\/strong> A bucket can contain up to 24 hours\u2019 worth of events.<\/li>\n\n\n\n<li><code><strong>hours<\/strong><\/code><strong>.<\/strong> A bucket can contain up to 30 days\u2019 worth of events.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>For example, if the <code>granularity<\/code> parameter is set to <code>minutes<\/code>, the same bucket can include documents with the timestamps <code>2024-12-10T12:09:43Z<\/code>, <code>2024-12-10T14:09:43Z<\/code>, and <code>2024-12-10T16:09:43Z<\/code>, but not <code>2024-12-11T12:09:43Z<\/code>, which is one day later.<\/p>\n\n\n\n<p>You can set the <code>granularity<\/code> parameter either by including it when creating the collection or when modifying the collection\u2019s definition. For example, if you were creating the <code>pressure<\/code> collection for the first time, you could use the following <code>createCollection<\/code> command, which sets the <code>granularity<\/code> parameter to <code>minutes<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.createCollection(\n  \"pressure\",\n  {\n    timeseries: {\n      timeField: \"timestamp\",\n      metaField: \"source\",\n      granularity: <strong>\"<\/strong>minutes<strong>\"<\/strong>\n    }\n  }\n);<\/pre>\n\n\n\n<p>Because the <code>pressure<\/code> collection already exists, you\u2019ll likely want to modify the collection definition rather than re-create it. To update the definition, use the <code>runCommand<\/code> database method to call the <code>collMod<\/code> database command, as shown in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.runCommand({\n  collMod: \"pressure\",\n  timeseries: { granularity: \"minutes\" }\n});<\/pre>\n\n\n\n<p>The command modifies the <code>timeseries<\/code> element by setting the <code>granularity<\/code> parameter to <code>minutes<\/code>. Nothing else will change in the collection\u2019s definition.<\/p>\n\n\n\n<p>In this case, modifying the collection definition is no problem. However, MongoDB places limits on how you can modify a time series collection. For example, when you modify the granularity, you cannot go from a longer timespan to a shorter one. In other words, you can move from <code>seconds<\/code> to <code>minutes<\/code>, but not from <code>minutes<\/code> to <code>seconds<\/code>.<\/p>\n\n\n\n<p>MongoDB also places limitations on the <code>metaField<\/code> parameter. You cannot change the parameter\u2019s value to a different field. However, if the field is an object (embedded document), you can add subfields to the documents.<\/p>\n\n\n\n<p>In some cases, you might want to have more control over your collection\u2019s granularity than what is provided through the <code>granularity<\/code> parameter. You can achieve this by using the following two custom parameters, rather than the <code>granularity<\/code> parameter:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><code><strong>bucketMaxSpanSeconds<\/strong><\/code><strong>.<\/strong> Specifies the maximum time between timestamps for documents in the same bucket.<\/li>\n\n\n\n<li><code><strong>bucketRoundingSeconds<\/strong><\/code><strong>.<\/strong> Sets the minimum time for a new bucket by rounding down the document\u2019s timestamp.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>The two parameters were introduced in MongoDB 6.3. Each one takes a single argument, the number of seconds. In addition, the two arguments must be the same value. For example, if you were creating the <code>pressure<\/code> collection from scratch, you might use the following <code>createCollection<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.createCollection(\n  \"pressure\",\n  {\n    timeseries: {\n      timeField: \"timestamp\",\n      metaField: \"source\",\n      bucketRoundingSeconds: 86400, \n      bucketMaxSpanSeconds: 86400\n    }\n  }\n);<\/pre>\n\n\n\n<p>The command sets the value of each customer parameter to <code>86400<\/code> seconds (24 hours). However, given that the collection already exists, you\u2019ll likely want to use the <code>runCommand<\/code> method instead, as in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.runCommand({\n  collMod: \"pressure\",\n  timeseries: { \n    bucketRoundingSeconds: 86400, \n    bucketMaxSpanSeconds: 86400\n  }\n});<\/pre>\n\n\n\n<p>If you use the custom parameters when updating your collection\u2019s granularity, you must adhere to the same restrictions as with the <code>granularity<\/code> parameter. That is, you cannot move from a longer timespan to a shorter one, although you can move from a shorter one to a longer one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-enable-automatic-removal-on-a-collection\">Enable automatic removal on a collection<\/h2>\n\n\n\n<p>By default, MongoDB retains the documents in a time series collection until they\u2019ve been manually deleted, similar to a conventional collection. However, you can override this behavior by including the <code>expireAfterSeconds<\/code> parameter in your collection definition. The parameter lets you specify the number of seconds that the documents should be retained before MongoDB automatically deletes them.<\/p>\n\n\n\n<p>You can specify the <code>expireAfterSeconds<\/code> parameter either when you create the collection or when updating the collection definition. Just to be complete, I\u2019ll first show you how to incorporate the option in your initial collection definition:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.createCollection(\n  \"pressure\",\n  {\n    timeseries: {\n      timeField: \"timestamp\",\n      metaField: \"source\",\n      bucketRoundingSeconds: 86400, \n      bucketMaxSpanSeconds: 86400\n    },\n    expireAfterSeconds: 43200\n  }\n);<\/pre>\n\n\n\n<p>The command specifies that the collection\u2019s documents should be removed after 43,200 seconds (12 hours). When a document\u2019s timestamp exceeds the specified threshold, MongoDB automatically deletes it from the database. The deletion might not occur immediately, but the document will eventually be removed. If all the documents in a bucket are deleted, MongoDB removes the bucket as well.<\/p>\n\n\n\n<p>Once again, because the <code>pressure<\/code> collection already exists, you\u2019ll likely want to use the <code>runCommand<\/code> method and <code>collMod<\/code> command to update the collection definition. For example, the following command enables automatic removal in the existing collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.runCommand({\n  collMod: \"pressure\",\n  expireAfterSeconds: 43200 \n});<\/pre>\n\n\n\n<p>There might be times when you want to disable automatic deletion on a time series collection. You can again use the <code>runCommand<\/code> method to update the collection definition. Only this time, you should set the <code>expireAfterSeconds<\/code> parameter to <code>off<\/code>, as in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.runCommand({\n  collMod: \"pressure\",\n  expireAfterSeconds: \"off\" \n});<\/pre>\n\n\n\n<p>After you run the command, the documents will no longer be automatically deleted from the <code>pressure<\/code> collection, although you can reenable automatic removal at any time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-getting-started-with-time-series-collection-in-mongodb\">Getting started with time series collection in MongoDB<\/h2>\n\n\n\n<p>A time series collection can be a handy tool for supporting workloads that record events at specific points in time, whether they\u2019re generated by stock trades, weather stations, industrial IoT sensors, smart devices in people\u2019s homes, or other places and systems. As handy as they are, however, time series collections are not suited to documents that are subject to frequent updates, such as those used in transactional processing.<\/p>\n\n\n\n<p>If you\u2019re supporting workloads that can benefit from a time series collection, you should invest the time in learning how they work and how best to optimize them, particularly when it comes to setting the collection\u2019s granularity. You can find more details about time series collections in the MongoDB topic <a href=\"https:\/\/www.mongodb.com\/docs\/manual\/core\/timeseries-collections\/\">Time Series<\/a>, which provides an in-depth look at how the collections work and how to get the most out of them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throughout this series, I\u2019ve introduced you to different features in MongoDB and provided examples to help demonstrate how the database system works. The examples have all been based on conventional collections, the type that MongoDB creates by default. However, MongoDB also supports other types of collections, including the time series collection, which can benefit many&#8230;&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":104933,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[53,159161],"tags":[5618,159226],"coauthors":[6779],"class_list":["post-104932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-mongodb","tag-mongodb","tag-mongodbseriesrobertsheldon"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/104932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=104932"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/104932\/revisions"}],"predecessor-version":[{"id":104934,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/104932\/revisions\/104934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/104933"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=104932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=104932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=104932"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=104932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}