Sunday, May 15, 2016


Capped collections 


Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read and delete operations. By circular, it means that when the fixed size allocated to the collection is exhausted, it will start deleting the oldest document in the collection without providing any explicit commands.

Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the order of the disk storage, it ensures that the document size does not increase the size allocated on disk. Capped collections are best for storing log information, cache data or any other high volume data.

db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }

If you want to check whether a collection is capped or not, use the following isCapped command:

db.cappedLogCollection.isCapped()

If there is an existing collection which you are planning to convert to capped, you can do it with the following code:

db.runCommand({"convertToCapped":"posts",size:10000})

This code would convert our existing collection posts to a capped collection.

Querying Capped Collection:

By default a find query on a capped collection will display results in insertion order. But if you want the documents to be retrieved in reverse order, use the sort command as shown in the following code:

db.cappedLogCollection.find().sort({$natural:-1})

There are few other important points regarding capped collections worth knowing:

We cannot delete documents from a capped collection.There are no default indexes present in a capped collection, not even on _id field.While inserting a new document, MongoDB does not have to actually look for a place to accommodate new document on the disk. It can blindly insert the new document at the tail of the collection. This makes insert operations in capped collections very fast.
Similarly, while reading documents MongoDB has just to return documents in the same order as present on disk. This makes the read operation very fast.

Mongo Server installation with Config file(BackGround run)


In this tutorial, I will show you how to install MongoDB on Windows.


  1. MongoDB 2.2.3
  2. Windows 7

1. Download MongoDB

Download MongoDB from official MongoDB website. Choose Windows 32 bits or 64 bits. Unzip, extracts to your prefer location, for example : d:\mongodb\.

2. Configuration File
Create a MongoDB config file, it’s just a text file, for example : d:\mongodb\mongo.config

Enter the below entries

dbpath=D:\mongodb\data

##all output go here
logpath=D:\mongodb\log\mongo.log

##Run mongo server in background
fork=true

NOTE:
MongoDB need a folder (data directory) to store its data. By default, it will store in “C:\data\db“,
create this folder manually. MongoDB won’t create it for you. You can also specify an alternate data directory with --dbpathoption.


3.Run MongoDB server

Use mongod.exe --config d:\mongodb\mongo.config to start MongoDB server.

For eg :-
d:\mongodb\bin>mongod --config D:\mongodb\mongo.config
all output going to: D:\mongodb\log\mongo.log

You Should get message something like below
about to fork child process, waiting until server is ready for connections.
forked process: <PID for mongo server>

4.Connect to MongoDB Server (by running client)

Above command on step 3 has started mongo server and it ready to receive connection from client.

Open new terminal and direct to bin folder of mongodb which was installed and run below command.

d:\mongodb\bin>mongo
 MongoDB shell version: 2.2.3
  connecting to: test
> //mongodb shell

on Server terminal you can see below message that it has received one connection.

2016-05-15T16:43:12.679+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:62866 #1 (1 connection now open)