MongoDB - Read - Interview Questions

How do you find documents from a collection?

 

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" },
{...}, ... , {...}

How do you find all documents from a collection?

 

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" }, {...}, ... , {...}

How do you search for exact field matches in MongoDB. i.e how do you find all documents that contains a field with a specific value? For example, how do you find all employees in the employee collection, whose ‘title’ is ‘Manager’?

 

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" }, {...}, ... , {...}

How do you search for documents in which a specific field have one or more values? For example, how do you find all employees in the employee collection, whose ‘title’ is either ‘Manager’ or 'supervisor?

 

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" }, {...}, ... , {...}

How do specify AND conditions when searching for MongoDB documents? For example, how do you find all employees in the employee collection, whose ‘title’ is ‘Manager’ AND 'age' is less than '30'?

 

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" }, {...}, ... , {...}

How do specify OR conditions when searching for MongoDB documents? For example, how do you find all employees in the employee collection, whose ‘title’ is ‘Manager’ OR 'age' is less than '30'?

 

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" }, {...}, ... , {...}

How do you search for MongoDB documents that specify both AND as well as OR conditions? For example, how do you find all employees in the employee collection, whose ‘dept’ is ‘IT’ AND either 'age' is less than '30' or 'title' is 'manager'?

 

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" }, {...}, ... , {...}
 
Subscribe to our Questions

 

MongoDB - Interview Questions

MongoDB - BasicsMongoDB - CreateMongoDB - ReadMongoDB - UpdateMongoDB - DeleteMongoDB - SearchMongoDB - AggregationMongoDB - Data ModelingMongoDB - Data ReplicationMongoDB - ShardingMongoDB - SecurityMongoDB - Administration
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example