MongoDB Query Document Using Find() Method (Examples)

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

MongoDB Query Document Using Find() with examples: Usage of Find by ID, Find in Array, Find all, Find and Update with examples

We learned about ObjectId and its methods within MongoDB in our previous tutorial.

In this tutorial, we will learn about the Usage of Find() in MongoDB.

Read through the MongoDB Online Tutorials for complete knowledge of the concept.

7. Find()

Find Method in MongoDB

Find method in MongoDB Query is used to fetch a particular document from the MongoDB collection. There are totally six methods available in Mongo DB by which we can fetch particular records.

Those methods include:

  • find()
  • findAndModify()
  • findOne()
  • findOneAndDelete()
  • findOneAndReplace()
  • findOneAndUpdate()

Syntax:

find(query,projection)

Find method consists of two parameters by which we can fetch a particular record. If we don’t use these two parameters then the find method will return all the documents available within the MongoDB collection.

Query – This is an optional parameter which defines the selection criteria. In simple words, what you want to search within a collection will be defined as a query.

Projection – This is an optional parameter which defines what to return when the query criteria successfully meet. In simple words, it is a kind of decision making which will take the decision upon the criteria.

Find All Documents

In order to fetch all the records that are available within a collection, we need to use the find method having an empty parameter. In simple words, we will not use any parameter when we need all the records.

Code

db.staff().find()

Figure 1: In Mongo Shell

Find All Documents in mongo db

Figure 2: In Robo 3T

Find All Documents in robo 3t

Find Specific Documents

In order to fetch a specific document from the MongoDB collection, we can use a query parameter in the find method. This parameter will help us to fetch only that record which meets the criteria.

In the following example, we are going to fetch the record by staff ID, where the staff ID is equal three and it will return us only that document.

Code

db.staff().find({staff_id:3})

Figure 3: In Mongo Shell

Find Specific Documents in mongo shell

Figure 4: In Robo 3T

Find Specific Documents in robo 3t

Find Documents with Specific Fields

In order to fetch specific fields, we have to use projection within the find method. As discussed earlier, projection is a kind of decision making. It takes the decision to show and hide fields.

In the following example, you can observe that we are only getting a record from the staff member name field.

Code

db.staff.find({},{_id:0,staff_member_name:1})

Figure 5: In Mongo Shell

Find with Specific Fields in mongo shell

Figure 6: In Robo 3T

Find with Specific Fields in robo 3t

Find Specific Documents with Conditional Criteria

We can use conditions to filter a specific record critically.

In the above examples, we discussed how to fetch a specific document. But now we will apply a condition on that specific document to return only those documents which successfully meet the condition.

In the following example, we are fetching the record by member name “Alex”, where the staff ID is greater than 2.

Code

db.staff().find({staff_member_name:”Alex”, staff_id:{$gt:2}})

Figure 7: In Mongo Shell

Find with Conditional Criteria mongo db

Figure 8: In Robo 3T

Find with Conditional Criteria in robo 3t

Conclusion

Find method is used to fetch a document from the MongoDB collection.

Using the Find method, we can fetch specific documents as well as the particular fields that we need. We can also use other find methods to retrieve specific documents as per our requirement.

Our upcoming tutorial will explain more about the usage of Cursor in MongoDB!!

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment