EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW