Time Series

An Example Time Series

The first pattern we are going to explore is the time series pattern. This pattern is a write optimization pattern to ensure maximum write performance for a typical analytics application. A time series is made up of discreet measurements at timed intervals. Examples can include counting the number of page views in a second or the temperature per minute. For this pattern we will discuss time series in the context of web page views.

| Schema Attributes | |——————–|——————————| | Optimized For | Write Performance | | Pre-Allocation | Benefits from Pre-Allocation |

To maximize our write performance for a time series we have are going to assume that we are interested in discreet buckets of time. That’s to say an individual page view is not interesting to the application, only the number of page views in a particular second, minute, hour, day or any time range in-between. This means the smallest unit of time we are interested in is a single minute.


Taking that into account let’s model a bucket to keep all our page views for a particular minute.

  "page": "/index.htm",
  "timestamp_minute": ISODate("2014-01-01T10:01:00Z"),
  "total_views": 0,
  "seconds": {
    "0": 0

Let’s have a quick view of what the fields mean.

Field Description
page The web page we are measuring
timestamp_minute The actual minute the bucket is for
total_views Total page views in this minute
seconds Page views for a specific second in the minute

As we can see the document represents not only a single minute of page views for a specific page but also allows for looking at individual seconds.

Update Page Views

Let’s simulate what happens in an application that is counting page views for a specific page. We are going to simulate updating a bucket for a specific page view in the 2nd second of the ISODate(“2014-01-01T10:01:00Z”) bucket.

use analytics
var secondInMinute = 2;
var updateStatment = {$inc: {}};
updateStatment["$inc"]["seconds." + secondInMinute] = 1;

  page: "/index.htm",
  timestamp_minute: ISODate("2014-01-01T10:01:00Z")
}, updateStatment, true)

The first part of the updateStatement sets up the $inc value to increment the field in the seconds field named 2 which corresponds with the second second in our time period. If the field does not exist MongoDB will set it to one otherwise it will increment the existing value with one. Notice the last parameter of the update statement. This is telling MongoDB to do an upsert, meaning MongoDB will create a new document if none exists. Let’s query for the document.

use analytics
  page: "/index.htm",
  timestamp_minute: ISODate("2014-01-01T10:01:00Z")

Returns the following document.

  "_id" : ObjectId("52de4ef8297f2f3b6f41d242"),
  "page" : "/index.htm",
  "timestamp_minute" : ISODate("2014-01-01T10:01:00Z"),
  "seconds" : {
    "2" : 1

Unfortunately there is a slight problem with this way of creating new buckets, namely that the document will grow over time causing MongoDB to have to move it around an incurring a write performance penalty. Luckily there is a work around to improve the write performance.


We can preallocate documents for our minute buckets. Let’s look at a simple little script that takes in a specific hour and pre-allocates minute buckets for that hour.

var preAllocate = function(coll, pageName, timestamp) {
  for(var i = 0; i < 60; i++) {
      "page": pageName,
      "timestamp_minute" : timestamp,
      "seconds" : {

    timestamp.setMinutes(timestamp.getMinutes() + 1);

Let’s take this pre-allocation method out for a test run.

var col = db.getSisterDB("analytics").page_views;
preAllocate(col, "index.htm", ISODate("2014-01-01T10:01:00Z"));

For this example we are dropping any existing documents in the page_views collection for clarity reasons. Now run the following commands.

var col = db.getSisterDB("analytics").page_views;

The col.count() returns 60 showing we have inserted 60 buckets. Looking over the results from the col.find() you’ll notice that each one has an incrementing timestamp and that the interval is 1 minutes.

With out pre-allocated documents, the update command will hit an existing empty bucket and since the bucket is at it’s maximum size it will never grow and MongoDB will avoid having to copy the data to a new location. This will increase the write performance as MongoDb can spend more of it’s time performing updates in place.