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.

Commands

To start MongoDB server we should use –

mongod –dbpath [Our MongoDB Data Directory]

Now to start the Database from shell –

mongo

To test current database

db

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

Insert

To insert collection use –

db.user.insert({“name”:”John”,”age”:23})
db.user.insert({“name”:”Rob”,”age”:25})

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({“name”:”John”}) 

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

db.user.find({“point”:{$gte:5,$lte:20}})
or
db.user.find({“point”:{$in:[5,20]}})

Next increase point of all user by 10
db.user.update({},{$inc:{“point”:10}},{multi:true})

And to decrease
db.user.update({},{$inc:{“point”:-10}},{multi:true})

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.
db.user.update({“name”:”Mary”},{$set:{“age”:30,”point”:50}},{upsert:true})

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
db.user.find({$and:[{“point”:50},{“age”:{$gte:25}}]})

Find users whose points are either 10 or 20
db.user.find({$or:[{“point”:10},{“point”:20}]})

Find users whose age not greater than 25
db.user.find({“age”:{$not:{$gt:25}}})

Next we will find and update those user whose age and point both lessor than 30 and give them category=junior
db.user.update({“age”:{$lt:30},”point”:{$lt:30}},{$set:{“category”:”junior”}},{multi:true})

Update salary of all users to 5000 whose points are either 10 or 20
db.user.update({$or:[{“point”:10},{“point”:20}]},{$set:{“salary”:5000}},{multi:true})

Update salary of all users to 10000 whose points are 50 and age >=25
db.user.update({$and:[{“point”:50},{“age”:{$gte:25}}]},{$set:{“salary”:10000}},{multi:true})

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

Sorting

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 *