Pages

January 27, 2020

#MongoDB Part 3: Interview Questions And Answers

How to query a document in MongoDB using find()?
db.collection.find () is used for retrieval of documents from a MongoDB database. The find command is an in-built function which is used to retrieve the documents in the collection.

To print all the employee's we can use below command, where 'Employee' is the collection name in the MongoDB database and '{ }' is the empty query document.

 db.Employeefind( { } )

To print the employee with name 'David Moore' we can use below query:

db.Employee.find({EmployeeName : "David Moore"}).forEach(printjson);

Now let's say we want to find for all Employee's whose id is greater than 7. In this case we can use $gt.

db.Employee.find({EmployeeID : {$gt:7}}).forEach(printjson);

What is a Cursor in MongoDB?
When the db.collection.find() function is used to search for documents in the collection, the result returns a pointer to the collection of documents returned which is called a cursor.

The cursor will be iterated automatically when the result of the query is returned. If required we can also explicitly go through the items returned in the cursor one by one.

Let's say we want to find the employees with employee id greater than 7 and we want to print it using cursor. For this we will declare the JavaScript variable 'empRS', which will hold the result set of the query. Then we will started a loop to iterate through all of the documents which are returned as part of the query.

var empRS =db.Employee.find({EmployeeID : {$gt:7}});

while(empRS.hasNext())
{
   print(tojson(empRS.next()));
}

Example of sort() and limit().
limit() can be used to limit the number of documents which are returned in the result set for a query.e.g:

db.Employee.find().limit(3).forEach(printjson);

Let's say we cant to sort the results in descending order. In the below example, the -1 indicates that we want to return the documents based on the descending order of EmployeeID.

db.Employee.find().sort({EmployeeID:-1}).forEach(printjson)

Example of count() and remove() functions.
Suppose we want to know what is the count of documents in a collection as per the query fired, then we can use the count() function of MongoDB. e.g:

db.Employee.count();

In MongoDB, the db.collection.remove() method is used to remove documents from a collection. Either all of the documents can be removed from a collection or only those which matches a specific condition. e.g: below code will remove employee with EmployeeID as 11.

db.Employee.remove({EmployeeID:11})

Example of MongoDB's update() command.
We can use update() command to update the documents of a collection. To update specific documents we can add a criteria to the update statement so that only selected documents are updated. e.g:

db.Employee.update(
{"EmployeeID" : 9},
{$set: { "EmployeeName" : "New David Moore"}});

To update multiple values:

{"EmployeeID" : 9},
{$set: { 
   "EmployeeName" : "New David Moore",
   "Age": 55
}});

-K Himaanshu Shuklaa..

No comments:

Post a Comment