MongoDB provides db.collections.find() operation to find documents in a collection. The syntax of find() operation is db.collections.find({query filter},{projection}).
You can enter search queries in 'query filter' section. For example {'title':'manager'} would return all employees whose title in 'manager'.
You can specify what data you want to see in the results. For example a projection of {'fname','lname'} would return only first name and last name from the resulting data.
//find all employees whose title is manager
>db.employees.find({"title":"Manager"})
{ "_id" : ObjectId("58a0abd281554bf3084e7ee0"), "fname" : "John",
"lname" : "Doe", "age" : "25", "title" : "Manager", "dept" : "IT" },
{...}, ... , {...}
You can find all the documents from a collection by using the find() operation without the query filter section. You can use db.collections.find() or db.collections.find({}) to find all the documents contained in a collection. For example, db.employees.find() returns all the documents contained in employees collection
>db.employees.find()
{ "_id" : ObjectId("588e54d4363650c07be0817b"),
"fname" : "John", "lname" : "Doe",
"age" : "25", "title" : "Manager",
"dept" : "IT" }, {...}, ... , {...}
You can use db.collections.find() operation and use filter condition specifying the field and value - db.collections.find({field:value}.
Below code returns all employees whose title is Manager.
>db.employees.find({"title":"manager"})
{ "_id" : ObjectId("588e54d4363650c07be0817b"),
"fname" : "John", "lname" : "Doe",
"age" : "25", "title" : "manager",
"dept" : "IT" }, {...}, ... , {...}
You can use db.collections.find() operation and use query operator $in specifying the field and values
Below code returns all employees whose title is 'manager' or 'supervisor'.
>db.employees.find({ title: { $in: ["manager" , "supervisor"] } } )
{ "_id" : ObjectId("588e54d4363650c07be0817b"),
"fname" : "John", "lname" : "Doe",
"age" : "25", "title" : "manager",
"dept" : "IT" }, {...}, ... , {...}
You can use db.collections.find() operation and use compound queries to specify conditions for more that one field in the collection's documents.
Below example finds all employees in the employee collection whose 'title' is 'manager' and 'age' is less than '30'.
>db.employees.find({ title: "manager", age: { $lt: 30 } } )
{ "_id" : ObjectId("588e54d4363650c07be0817b"),
"fname" : "John", "lname" : "Doe",
"age" : "25", "title" : "manager",
"dept" : "IT" }, {...}, ... , {...}
You can use db.collections.find() operation and use compound queries with $or operator to search for documents that match at least one condition.
Below example finds all employees in the employee collection whose 'title' is 'manager' OR 'age' is less than '30'.
>db.employees.find( { $or: [ { title: "manager" } , { age: { $lt: 30 } } ] } )
{ "_id" : ObjectId("588e54d4363650c07be0817b"),
"fname" : "John", "lname" : "Doe",
"age" : "25", "title" : "manager",
"dept" : "IT" }, {...}, ... , {...}
You can use db.collections.find() operation and use compound queries with $or operator to search for documents that match at least one condition.
Below example finds all employees in the employee collection whose 'dept' is 'IT' and either 'title' is 'manager' OR 'age' is less than '30'.
>db.employees.find( dept : "IT",
$or: [ { title: "manager" } , { age: { $lt: 30 } ] }
)
{ "_id" : ObjectId("588e54d4363650c07be0817b"),
"fname" : "John", "lname" : "Doe",
"age" : "25", "title" : "manager",
"dept" : "IT" }, {...}, ... , {...}