MongoDB Query Document using find() method

We will see the use of the find() method to query documents based on the given criteria.

Querying all the documents in JSON format

The syntax of Querying all the documents in JSON format −

 
\\mydbs is collection name  in a database named mydb.
db.mydbs.find()
------------------
------------------
\\To improve the readability,
db.mydbs.find().forEach(printjson);


Query Document based on the criteria

we are fetch selected documents based on a criteria.

Equality Criteria:If I want to fetch the data of “Smith†from mydbs collection.

\\Example of Equality Criteria
db.mydbs.find({StudentName : "Smith"}).pretty()


Greater Than Criteria:If I would like to fetch the details of mydbs having age > 28

\\Example of Greater Than Criteria
db.mydbs.find({"age":{$gt:28}}).pretty()


Less than Criteria:If all the mydbs having id less than 1500.

\\Example of Less than Criteria
db.mydbs.find({"StudentId":{$lt:150}}).pretty()


Not Equals Criteria:If all the mydbs where id is not equal to 1001.

\\Example of Not Equals Criteria
db.mydbs.find({"StudentId":{$ne:100}}).pretty()


Greater than equals Criteria:If all the mydbs where id is not equal to 1001.

\\Example of Greater than equals Criteria
db.mydbs.find({"StudentId":{$gte:200}}).pretty()


Less than equals Criteria:If all the mydbs where id is not equal to 1001.

\\Example of Less than equals Criteria
db.mydbs.find({"StudentId":{$lte:200}}).pretty()




Visit :


Discussion



* You must be logged in to add comment.