Query Document in MongoDB

mongoDBIn my previous post, I have discussed about MongoDb, how to install and how to create a database. In this post i am going to discuss about query document(It is like a record or row in relational databases) in MongoDB.

MongoDB Operator

Query Document in MongoDB

We already discussed, to get all the records

If you worked on MySQL then what you do when you want to retrieve only records with gender ‘M’



Let’s search with multiple criteria

Find all the rows with gender ‘M’ and designation ‘TL’

View only firstname and dob

Subscribe Our Tutorials

Get Latest Updates on Facebook


In MongoDB

Getting all data except dob

To view in proper format

NOTE: you can’t mix the including and excluding thing in same object. Only exception is for _id.


Let’s demonstrate this

I am excluding _id and including dob. It works fine

Here check by excluding firstname and including dob. It throws an error.

Sorting in MongoDB

MongoDB provides sort command to sort the records.

For Ascending:1
For Descending:-1

To Put Limit on Records

Updating Records in MongoDB

Update employee table with new field salary

Remove Records

Remove records of an employee whose firstname is raj

Remove all record from emp

In next tutorial I will discuss about mongodboperator.


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.