Useful mongoDB commands

To show database
show dbs

To use database
use <database-name>
example: use test1

To create new database
use <new database name>
example: use tst2

To show collection
show collections (use database before showing collection)

To delete collection
db.chauni.drop()

To list data stored in collection
db.<collection-name>.find()
example: db.designations.find()

To list data stored in collection in nice/easy format:
db.<collection-name>.find().pretty()
db.employees.find().pretty()

To create collection:
db.createCollection(“<collection-name>”)
example: db.createCollection(“chauni”)
Note: mongo is case sensitive. If you type C as small then it will show error.

To insert document in collection
db.<collection-name>.insert({………….})
example:
db.chauni.insert({
name: ‘shristi’,
address: ‘kimdol’
})

Here name and address are keys
Make sure after colon of key there is space. otherwise it will throw error.
Everything that is submitted is in JSON format and once it goes to database it will be automatically converted to BSON.
If data is provided in quote then that is taken as string.
If data is provided without quote then that is integer/number.

Few optional things are:
You can use double quote instead of single quote.
You can also use quote in key (eg: name)
You can use semicolon after last bracket.

If you view just created document then you will see following:
{ “_id” : ObjectId(“56a887c51e20886bc5228a31”), “name” : “shristi”, “address” : “kimdol” }
where id is auto generated by mongo.

To modify value of existing document field
db.<collection-name>.update({………….})
example:
db.chauni.update({
name: ‘sneha’
},
{$set: {
name: ‘uttama’
}
})

To add new key and value in the document
db.chauni.update({
name: ‘neha’
},
{$set: {
phone: ‘9803836863’
}
},{$upsert: true})
To remove all documents from collection
db.chauni.remove({})
This will remove all the documents inside the collection

To remove specific documents from collection
db.chauni.remove({
name: ‘sneha’
})

Some important features of Mongo shell is it works as a Javascript interpreter. You can also write code in mongoshell.

Example – using for loop to insert data
for(var i = 0; i<10; i++) {db.test01.insert({“x”: i})}
Output:
{ “_id” : ObjectId(“56a897dee5f82f610d7b394f”), “x” : 0 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3950”), “x” : 1 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3951”), “x” : 2 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3952”), “x” : 3 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3953”), “x” : 4 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3954”), “x” : 5 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3955”), “x” : 6 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3956”), “x” : 7 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3957”), “x” : 8 }
{ “_id” : ObjectId(“56a897dee5f82f610d7b3958”), “x” : 9 }
Querying data in mongoDB

To query all documents
db.<collection-name>.find()
example:
db.employee.find()
This shows all data

To query specific data:
db.employees.find({‘username’: ‘areddy’})
This will show the document having username areddy

To query document having username greater than u
db.employees.find({username: {$gt:’u’}}).pretty()

To query data having value greater than 20 in size field
db.test01.find({size: {$gt:20}})

To query data having value less than 20 in size field
db.test01.find({size: {$lt:20}})

To query using data of Array item
db.chauni.find({classes: {$in: [‘History’]}});
This will show those document having History in their classes field.

To query and sort using size field in ascending order
db.test01.find().sort({size: 1})
Here 1 means ascending. And size has integer value

To query and sort using size field in descending order
db.test01.find().sort({size: -1})
Here -1 means descending. And size has integer value

To query and sort for strings
> db.chauni.find().sort({name: 1}) //ascending
> db.chauni.find().sort({name: -1}) //decending
Here name field has string value

To show only two results
db.chauni.find().limit(2)
Instead of 2 you can write any number and that much amount of documents appears.

To query username starting from letter sru
db.employees.find({username: /^sru/}).pretty()

To update everyone’s email address to uttam.shrestha@javra.com
db.employees.update({},{‘$set’:{‘contact_details.email_id.official’:’uttam.shrestha@javra.com’}},{multi:true})

Leave a Reply

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