EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB find

MongoDB find

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB find

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

MongoDB find Example 1

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:

MongoDB find Example 2

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:

MongoDB find Example 3

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:

MongoDB find Example 4

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:

findOne Method Example 5

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:

AND condition Example 6

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:

Orcondition Example 7

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 –

  1. MongoDB Limit()
  2. MongoDB With Characteristics
  3. MongoDB Limit()
  4. MongoDB Delete
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1
 20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more