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.
Table of Contents:
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
Figure 2: 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
Figure 4: 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
Figure 6: 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
Figure 8: 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!!