Using MongoDB for capped collections on Windows


Any commands at the Mongo console are prefixed with a > symbol – otherwise you can assume the command is entered at the command line under the /bin directory.

Setting up your database:

Download the MongoDB binaries and follow the quick start guide here.

Follow all the instructions. You can then install Mongo as a service using the following sample command:

mongod --logpath c:\mongodb\logs\mongo.log --dbpath c:\mongodb\data\db --directoryperdb --install

Next run the service:

net start MongoDB

The next thing to do is to make sure the service is running OK and we can do that using the command line shell. So create a few collections in the (default) test database and make sure everything is OK. You’ll see a few files created in your c:\mongodb\data\db directory. You’ll also see some logging in the c:\mongodb\logs\ directory.

Creating a database:

To create a database you just need to switch to that database. For example to create a ‘logging’ database just switch to the database and create a new collection by simply saving one, for example:

>use logging;
>{ name : "dillon"});

Capped collections:

What I want to use Mongo for – just for now – is logging. I have a task that runs every few minutes and sends a lot of emails. I want to be able to log each and every email along with who we sent it to and the type of email. This is where Mongo’s capped collections come in. This type of collection will have a max size and older information will be discarded – like a self-managing log.

I want to create a 100MB capped collection, so to do that I need to allocate 104857600 bytes. But I can also cap the number of objects in the collection. So I’ll say I only want a maximum of 10,000 objects in my collection. So from the console I can run:

>db.createCollection("myLoggingCollection", {capped:true, size:104857600, max:10000});

I can then run the following command to ensure it has been created:

>show collections;


Mongo allows us create users for each database. So lets set that up. From the console run the following to set up the admin user:

>use admin;
>db.addUser("myUser", "myPassword");

And then the following to set up a user for our logging database:

>use logging;
>db.addUser("myUser", "myPassword");

Next we will need to re-install the Mongo service with the --auth flag so it looks for these credentials. So from the command line in the bin directory:

mongod --remove
mongod --logpath c:\mongodb\logs\mongo.log --dbpath c:\mongodb\data\db --directoryperdb --auth --install

Connecting to other Mongo servers:

If you wish to log onto a different Mongo server (i.e. not your localhost) then you’ll need to do something like this from your local console:

>conn = new Mongo("");
>db = conn.getDB("logging");
>use logging;
>db.auth("myUser", "myPassword");
>show collections
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to Using MongoDB for capped collections on Windows

  1. Pingback: MongoDB with ColdFusion – {“some”: “notes”} | Fly Dillon Fly

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s