A quick guide to MongoDB to work with

We have used some useful commands of MongoDB in our project work.Here we would like to discuss most of them so that they can be used for quick reference or quick guide.These commands can be executed from MongoDB shell.

Before we jump in

  • You can download MongoDB from here.
  • We are skipping the installation part which you can find here.
  • We will proceed with an example of simple user database (e.g. userdb) used to store information about users.

Let’s go for it.


To start MongoDB server we should use –

mongod –dbpath [Our MongoDB Data Directory]

Now to start the Database from shell –


To test current database


Show all existing databases

show dbs or show databases //This will display all the databases that exists previously with memory consumed.

Now to change or create new database we should use

use [Database you want to create or change] //For our example we would write use userdb


To insert collection use –


We are using user as collection.Now to see the documents –

show collections //This will display the list of documents in current database

Search and Update

db.user.find() //To display collections in a document
db.user.find().pretty() //shows the document in a well formatted manner
db.users.find(0) //Find collection by index
db.users.findOne() //Find first collection in a document

//Now Find doucment in collection by json elements

db.user.find({“age”:{$gte:24}}) //This will find users whose age are >=24

db.user.update({},{$set:{“point”:0}},{multi:true}) //Update points of all users and set to 0

db.user.update({“age”:{$gt:23}},{$set:{“point”:10}},{multi:true}) //Update and set points=10 for all users whose age >23

Now, Find user whose point is between 5 and 20


Next increase point of all user by 10

And to decrease

Update all user whose age less than 30 and set adbout=”This is user is young”
db.user.update({age:{$lt:30}},{$set:{“about”:”This user is young”}},{multi:true})

Now update the user whose name is ‘Mary’ and set point to 50 and age to 30.If the user doesn’t exist insert a new one.

Here Mary named user was not existing ,so it has been inserted as we used upsert(update or insert) option

Let’s make use of $and, $or, $not

Find all users whose points are 50 and age >=25

Find users whose points are either 10 or 20

Find users whose age not greater than 25

Next we will find and update those user whose age and point both lessor than 30 and give them category=junior

Update salary of all users to 5000 whose points are either 10 or 20

Update salary of all users to 10000 whose points are 50 and age >=25

Search using regular expression and pattern matching

Search user whose name starts with ‘j’ (Case sensitive)
db.user.find({ name: { $in: [ /^j/ ] } })

Search user whose name starts with ‘j’ or ‘m’ (Not case sensitive)
db.user.find({ name: { $in: [ /^j/i , /^m/i, ] } })

Search user whose name contain ‘oh’ (Not case sensitive)
db.user.find({ name: { $regex: /oh/i } })

Search user whose about contains ‘is Young'(Not case sensitive)
db.user.find({ about: { $regex: /is Young/, $options: ‘i’ }})

*** $options: ‘i’ means search is not case sensitive


Sort the user collection on the field age in ascending order
db.user.aggregate([{ $sort : { age : 1} }])

To sort user on age descending order and points ascending order together,
db.user.aggregate([{ $sort : { age : -1, points: 1 } }])

If you find this article helpful, you can connect us in Google+ and Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *