MongoDb. Capped collections

Today we’re going to discuss not really popular, but really useful thing in MongoDB – capped collections. What’re they, how and where to use them. Let’s start!

The main thing that you need to know about capped collections at first that’s set of restrictions – they don’t have all the functionality of common collections:

  1. No delete operations;
  2. All elements in collection should have an equal size (update will be rejected if element has different size than it used to have for example);
  3. It works as queue and because of this no index is required;

The large set of restrictions, some of them can be lethal for normal collection usage for obvious reasons, so then exactly can we use such restricted set then? Let’s discuss some specifics in mechanics before make a decision and come to the conclusion.

Mechanic differences provide us following profits:

  1. More than 10 000 read/write operations per second (that’s really good performance for MongoDB);
  2. Cursor that can be used for tracking of new entry adding to collection;
  3. Auto-removing from collection according to FIFO rule than size exceeds given limitations;
MongoDb. Capped collections. Profit

MongoDb. Capped collections. Profit

I believe now it comes to be more interesting for you, so let’s proof that this functionality is actually used, and the best example at this point is that mongo actually uses it by itself!
Local.startup_log – collection that stores all the MongoDB settings on start is actually a capped one.

Oplog – collection that stores logs about all your operation and sits behind everything that can be done in MongoDB.

So MongoDB uses those collections by itself, that allows it be efficient in many points. I believe that after those examples we definitely need to try this thing by ourselves. But where? In my mind the basic points of usage for such collections can be caching and logging, because for both cases it’s fast and can clean memory by itself.

So let’s do some coding and see how to use it with Node.js. First of all we need to initialize this collection and there’re differences with common collection already, you need to set capped flag to true and specify amount that collection can take from memory (as you remember it’s required for self cleaning and efficient operations upon the data).

So as soon as we’re done with collection initialization let’s do something with this one. And here we’re going to initialize cursor as was discussed before. Cursor is a special entity that allows us to track adding operations to collection and can be useful for different things, for example, you tracked adding of fatal log error to logs, inside cursor can sit logic that will send email to responsible admin/programmer of this part of code.

Be free to play with this code and you will understand much better how the capped collections work. Let’s summarize now what’re the differences here:

MongoDb. Capped collections. Summarizing

MongoDb. Capped collections. Summarizing

Now I believe you know the main scope of usage for capped collection, how to initialize and work upon them. Be free to ask questions in comments and offer your best cases of usage. Good luck!

We are looking forward to meeting you on our website

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles


How To Use Prospector For Python Static Code Analysis

Prospector is a tool that analyzes Python code, outputs information about the errors, potential problems, convention violation, and complexity of the ...


Fluent Validation in ASP.NET MVC

Fluent validation is a validation library for .NET. This will be separated from the business logic. So, it has an advantage over data annotation ...

How to implement WPF Canvas? Explain with an example

Canvas is a lightweight layout of WPF. Canvas is used for 2D graphic design elements but not for UI. You cannot use it for making text-box, checkbox, ...

No comments yet

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy