Create Collection in MongoDB

db.createCollection(name, options) is used by MongoDB to create collection.
Where name is name of collection to be created and Options is a document which is use to specify
 Name parameter is string type; here we put the name of collection.
Options parameter is document which Specify options about memory size and indexing etc. Options parameter is optional, so you need to specify only name of the collection. Following is the list of options you can use:




 Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.

If true, automatically create index on _id field.s Default

If capped is true it’s specify a maximum size in bytes for a capped collection, and then you need to specify this field also.

This allows the maximum number of documents allowed in the capped collection.

While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.
Following I am trying to write basic syntax for createCollection() method.

>use test
switched to db test
{ "ok" : 1 }

You can check the created collection by using the command show collections

>show collections

Following example shows the syntax of createCollection() method with few important options:

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

In mongodb you don't need to create collection. MongoDB creates collection automatically, when you insert some document.

>db.Codefari.insert({"name" : "Codefari"})
>show collections

Drop Database in MongoDB

The dropDatabase() Method
MongoDB db.dropDatabase() command is used to drop a existing database.
By using the command ‘show dbs’ check the available list of databases

>show dbs
local      0.78234GB
myMongodb       0.343012GB
test       0.43012GB

If you want to delete new database <myMongodb>, then use dropDatabase() command as below.

>use myMongodb
switched to db myMongodb
>{ "dropped" : " myMongodb ", "ok" : 1 }

Now check list of databases

>show dbs
local      0.78234GB
test       0. 43012GB

Note: This will delete the selected database. If you have not selected any database, then it will delete default 'test' database

Create Database in MongoDB

Using ‘Use’ keyword we can create database in MongoDB, for example see following command. 

>use myMongoDB

Command will create new database if “myMongoDB” does not exist otherwise it will return the existing database.
To check your currently selected database use the command db


If you want to check your databases list, then use the command show dbs.

>show dbs
Local 0.78987GB
myMongoDB 0.98978GB

How to Install MongoDB on Windows

You can download the latest release of MongoDB from, Make sure you get correct version of MongoDB depending upon your windows version.
32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes.
Now extract your downloaded file in any drive suppose we extract it in c:\\ drive, after extract file you will see a folder “mongodb-win32-x86_64-[version]” Here [version] is the version of MongoDb downloaded. You can rename this file name.
Now open the command prompt and run the following command.

[Your mongodb file location]\bin mongod 
 for example  C: \MongoDB\bin mongod

MongoDB require the folder “data\db” inside MongoDb folder location, the default location for the MongoDB data directory is c:\ MongoDB \data\db.
If you have install the MongoDB at different location, then you need to specify any alternate path for \data\db by setting the path dbpath in mongod.exe. For the same issue following commands
In command prompt navigate to the bin directory present into the mongodb installation folder. Suppose my installation folder is D:\set up\mongodb.

D:\>cd "set up"
D:\set up>cd mongodb\bin
D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"

If everything going well it will show a message “Waiting for Connection”. Now open a new command prompt and run following command.

D:\set up\mongodb\bin>mongo.exe
MongoDB shell version: 2.4.7
Use test
> { a: 1 } )
{ "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }

This will show that mongodb is installed and run successfully. Next time when you run mongodb you need to issue only commands.

D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"
D:\set up\mongodb\bin>mongo.exe

Note: 32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes.

PostgreSQL-Query: Sort result set by specific field values using ORDER BY Clause

Problem: Suppose we have a book_inventory table which has some columns such as id, isbn, title, author, publisher, publish_date, etc.. whe...