Loading

Mongo DB

MongoDB Query Document: db.collection.find() with Example. The Complete Mongo DB Developer Course 2023 [Videos].

The method of fetching or getting data from a MongoDB database is carried out by using MongoDB queries. While performing a query operation, one can also use criterias 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 MongoDB query tutorial, you will see how this function is used in various ways to achieve the purpose of document retrieval.

MongoDB Basic Query Operations

The basic MongoDB query operators cover the simple operations such as getting all of the documents in a MongoDB collection. Lets look at an db.collection.find 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.

MongoDB Basic query operation

MongoDB Basic query operation

Code Explanation:

  1. Employee is the collection name in the MongoDB database
  2. The MongoDB find query is an in-built function which is used to retrieve the documents in the collection.

If the command is executed successfully, the following Output will be shown for the MongoDB find example

Output:

MongoDB Basic query operation Example

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.

MongoDB Query Example – 1

Lets look at a couple of MongoDB query examples of how we can accomplish this.

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

Code Explanation:

  1. Here we want to find for an Employee whose name is “Smith” in the collection , hence we enter the filter criteria as EmployeeName : “Smith”

If the command is executed successfully, the following Output will be shown

Output:

MongoDB Basic find query operation Example

The output shows that only the document which contains “Smith” as the Employee Name is returned.

MongoDB Query Example – 2

Now in this MongoDB queries tutorial, lets 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:

  1. Here we want to find for all Employees whose id is greater than 2. The $gt is called a query selection operator, and what is just means is to use the greater than expression.

If the MongoDB select fields command is executed successfully, the following Output will be shown

Output:

MongoDB find() query operation Example

All of the documents wherein the Employee id is greater than 2 is returned.

See All

Comments (195 Comments)

Submit Your Comment

See All Posts

Related Posts

Mongo DB / Youtube

What is MongoDB and why use it?

MongoDB is an open source NoSQL database management program. NoSQL is used as an alternative to traditional relational databases. NoSQL databases are quite useful for working with large sets of distributed data. MongoDB is a tool that can manage document-oriented information, store or retrieve information.
27-dec-2020 /4 /195

Mongo DB / Youtube

What is the difference between a document and a collection in MongoDB?

Databases, collections, documents are important parts of MongoDB without them you are not able to store data on the MongoDB server. A Database contains a collection, and a collection contains documents and the documents contain data, they are related to each other.
27-jan-2021 /4 /195

Mongo DB / Youtube

What is MongoDB and why use it?

MongoDB is an open source NoSQL database management program. NoSQL is used as an alternative to traditional relational databases. NoSQL databases are quite useful for working with large sets of distributed data. MongoDB is a tool that can manage document-oriented information, store or retrieve information.
24-May-2021 /4 /195