Updated February 28, 2023
Introduction to MongoDB find() Method
MongoDB find is used to query data from collections if we need to retrieve documents from collection same time we have used find method in MongoDB. We can use the pretty method with find to the retrieved results of documents in a formatted way, a pretty method with find displays the record in a formatted way. The find method is beneficial and important to retrieve documents from the collection. If we retrieve all documents from the collection then we have used a find method if we retrieve a specific number of documents, then we have used find method with a limit in MongoDB.
Syntax of MongoDB find()
Below is the syntax of the find method.
- collection_name.find(): Display all the documents from collection.
- collection_name.findOne(): Display only one document from collection.
- collection_name.find().pretty(): Find method with pretty method.
- collection_name.find().limit(): Limit is used with find method.
- collection_name.find(query, projection)
Parameters:
Below is the parameter description of the above find syntax.
- Collection name: Collection name is defined as retrieved documents from the collection by using the find method. We can retrieve single and multiple documents by using find and findone method.
- Find: Find method is handy and important to retrieve documents from the collection. If we want to retrieve all documents from the collection, we have used a find method, we have also used limits to find methods to retrieve a specific number of the document from collections.
- FindOne: Find one method is used to retrieve the only a single document from the collection. If we want to retrieve a single document from the collection, then we have used findone method.
- Pretty: We have used a pretty method with find. Using a pretty method, we can display the documents in a formatted way.
- Limit: We can use limits with find methods in MongoDB. Using limit with find method, we can display the number of documents as per limit basis.
- Query: This is an optional parameter of find method in MongoDB. We can select the filter using the query operator in MongoDB. If we want all documents from the collection, then we have to skip this parameter.
- Projection: This is an optional parameter of find method in MongoDB. It will specify that fields from the collection which return from documents that match the query filter. If we want to return all documents, then this parameter was skipped.
How find Command Works in MongoDB?
Below is the working of find command
1. If we want to retrieve documents from the collection on a condition basis, then we have to use the following operator are as follows.
- Equality
- Less than
- Less than equals
- Greater than
- Greater than equals
- Not equals
- Values in an array
- Values not in an array
2. We can use the pretty method with find to retrieved results of documents in a formatted way, the pretty method with find displays the record in a formatted way
3. We can use a limit with the find method Using limit with find method, we can display the number of documents as per limit basis.
4. If we retrieve all documents from the collection then we have used a find method, if we retrieve only single documents then we have used find method with limit,
5. This command is used to query data from collections if we need to retrieve documents from collection same time we have used find method.
6. If we want to retrieve documents based on and condition, then we need to use $and keyword.
7. If we want to retrieve documents based on or condition, then we need to use $or keyword.
8. If we want to retrieve documents based on not condition, then we need to use $not keyword.
9. Findone method is used to retrieve only a single document from the collection. If we want to retrieve a single document from the collection, then we have used findone method.
10. Find method is handy and important to retrieve documents from the collection.
Examples
Below are the examples mentioned :
We have taken the example of emp_count table to describe an example of find methods are as follows. Below is the data description of the emp_count table are as follows.
Query:
db.emp_count.find ()
Output:
1. Retrieve all Documents Using find Method
In the below example, we have to retrieve all documents from emp_count table using find method in MongoDB. We have not used any query and projection to retrieve data.
Query:
db.emp_count.find ()
Output:
2. Find Method with Limit in MongoDB
In the below example, we have to retrieve a specific number of documents from the emp_count table using the find method with a limit.
We have retrieving only 3 documents of the emp_count table by using limit with find method.
Query:
db.emp_count.find (). limit (3)
Output:
3. Find with Pretty Method in MongoDB
In the below example, we have to retrieve all documents from emp_count table by using find with the pretty method in MongoDB.
Query:
db.emp_count.find ().pretty () .limit (3)
Output:
4. Retrieve Single Document Using findOne Method in MongoDB
In the below example, we have to retrieve single documents from the emp_count table using the findOne method in MongoDB. We have not used any query and projection to retrieve data.
Query:
db.emp_count.findOne ()
Output:
5. Find using AND condition in MongoDB
Below example show and condition with the find method in MongoDB.
Query:
db.emp_count.find ({$and :[{ emp_id: 7}, {emp_name: "CBA"}]})
Output:
6. Find Using orcondition in MongoDB
The below example shows or conditions with find method in MongoDB.
Query:
db.emp_count.find ({$or :[{ emp_id: 7}, {emp_name: "CBA"}]})
Output:
Conclusion
Find is used to query data from collections; if we need to retrieve documents from the collection, we have used find method in MongoDB. We can use the pretty method find to the retrieved result of documents in a formatted way. Find method is essential and useful in MongoDB.
Recommended Article
This is a guide to MongoDB find. Here we discuss the Introduction to MongoDB find() Method and its different subquery expressions and parameters. You can also go through our other suggested articles to learn more –