MongoDB
MongoDB Security, Monitoring & Backup (Mongodump)
One of the key concepts in MongoDB is the management of databases. Important aspects such as...
The method of fetching or getting data from a MongoDB database is carried out by using queries. While performing a query operation, one can also use criteria’s or conditions which can be used to retrieve specific data from the database.
MongoDB provides a function called db.collection.find () which is used for retrieval of documents from a MongoDB database.
During the course of this tutorial, you will see how this function is used in various ways to achieve the purpose of document retrieval.
The basic query operations cover the simple operations such as getting all of the documents in a MongoDB collection. Let’s look at an example of how we can accomplish this.
All of our code will be run in the MongoDB JavaScript command shell. Consider that we have a collection named ‘Employee’ in our MongoDB database and we execute the below command.
Code Explanation:
If the command is executed successfully, the following Output will be shown
Output:
The output shows all the documents which are present in the collection.
We can also add criteria to our queries so that we can fetch documents based on certain conditions.
Let's look at a couple of examples of how we can accomplish this.
db.Employee.find({EmployeeName : "Smith"}).forEach(printjson);
Code Explanation:
If the command is executed successfully, the following Output will be shown
Output:
The output shows that only the document which contains "Smith" as the Employee Name is returned.
Now, let's take a look at another code example which makes use of the greater than search criteria. When this criteria is included, it actually searches those documents where the value of the field is greater than the specified value.
db.Employee.find({Employeeid : {$gt:2}}).forEach(printjson);
Code Explanation:
If the command is executed successfully, the following Output will be shown
Output:
All of the documents wherein the Employee id is greater than 2 is returned.
One of the key concepts in MongoDB is the management of databases. Important aspects such as...
What is MongoDB? MongoDB is a document-oriented NoSQL database used for high-volume data storage....
What is Cursor in MongoDB? When the db.collection.find () function is used to search for documents in...
What is Query Modifications? Mongo DB provides query modifiers such as the 'limit' and 'Orders' clause...
The installers for MongoDB are available in both the 32-bit and 64-bit format. The 32-bit...
Aggregation basics --> The concept of aggregation is to carry out a computation on the results...