MongoDB Query Document using find() method
In my previous tutorials I have used the find() method to query all the documents from a collection.
In this tutorial, we will see the usage of find() method to query the documents from a collection based on the given criteria. Lets get started.
Querying all the documents in JSON format
Lets say we have a collection “employee” in a database named “employeeDB”. To get all the documents we use this command:
db.employee.find()
However the output we get is not in any format and less-readable. To improve the readability, we can format the output in JSON format with this command:
db.employee.find().forEach(printjson);
OR simply use pretty() – It does the same thing.
db.employee.find().pretty()
Query Document based on the criteria
Instead of fetching all the documents from collection, we can fetch selected documents based on a criteria.
Example Collection :
db.employee.find()
{ "_id" : ObjectId("5b3b70096728a1b55ecec85d"), "Employeeid" : 5, "dept_id" : 3, "EmployeeName" : "Mahesh", "location" : [ { "city" : "Chennai", "state" : "Karnataka" } ] }
{ "_id" : ObjectId("5b3b70706728a1b55ecec85e"), "Employeeid" : 1, "dept_id" : 2, "EmployeeName" : "Mohan", "location" : [ { "city" : "Trichy", "state" : "TN" } ] }
{ "_id" : ObjectId("5b3b70706728a1b55ecec85f"), "Employeeid" : 2, "dept_id" : 2, "EmployeeName" : "James", "location" : [ { "city" : "Chennai", "state" : "TN" } ] }
1.Equality Criteria:
For example: I want to fetch the data of “Mohan” from ’employee’ collection. The command for this should be:
db.employee.find({EmployeeName : “Mohan”}).pretty()
This command returns the document matching the given criteria.
{
"_id" : ObjectId("5b3b70706728a1b55ecec85e"),
"Employeeid" : 1,
"dept_id" : 2,
"EmployeeName" : "Mohan",
"location" : [
{
"city" : "Trichy",
"state" : "TN"
}
]
}
2.Greater Than Criteria:
Syntax:
db.collection_name.find({“field_name”:{$gt:criteria_value}}).pretty()
For example: I would like to fetch the details of ’employee’ having dept_id > 2 then the query should be:
db.employee.find({“dept_id”:{$gt:2}}).pretty()
db.employee.find({"dept_id":{$gt:2}}).pretty()
{
"_id" : ObjectId("5b3b70096728a1b55ecec85d"),
"Employeeid" : 5,
"dept_id" : 3,
"EmployeeName" : "Mahesh",
"location" : [
{
"city" : "Chennai",
"state" : "Karnataka"
}
]
}
3.Less than Criteria:
Syntax
db.collection_name.find({“field_name”:{$lt:criteria_value}}).pretty()
Example: Find all the employee having dept_id less than 3. The command for this criteria would be:
> db.employee.find({"dept_id":{$lt:3}}).pretty()
{
"_id" : ObjectId("5b3b70706728a1b55ecec85e"),
"Employeeid" : 1,
"dept_id" : 2,
"EmployeeName" : "Mohan",
"location" : [
{
"city" : "Trichy",
"state" : "TN"
}
]
}
{
"_id" : ObjectId("5b3b70706728a1b55ecec85f"),
"Employeeid" : 2,
"dept_id" : 2,
"EmployeeName" : "James",
"location" : [
{
"city" : "Chennai",
"state" : "TN"
}
]
}
4. Not Equals Criteria:
Syntax:
db.collection_name.find({“field_name”:{$ne:criteria_value}}).pretty()
Example: Find all the ’employee’ where dept_id is not equal to 2. The command for this criteria would be:
db.employee.find({“dept_id”:{$ne:2}}).pretty()
db.employee.find({"dept_id":{$ne:2}}).pretty()
{
"_id" : ObjectId("5b3b70096728a1b55ecec85d"),
"Employeeid" : 5,
"dept_id" : 3,
"EmployeeName" : "Mahesh",
"location" : [
{
"city" : "Chennai",
"state" : "Karnataka"
}
]
}
The other two criteria:
Greater than equals Criteria:
db.collection_name.find({"field_name":{$gte:criteria_value}}).pretty()
Less than equals Criteria:
db.collection_name.find({"field_name":{$lte:criteria_value}}).pretty()