The db.collection.find()
method in MongoDB is used to retrieve all documents or a specific document that matches the criteria from a collection. By default, it returns 20 records/documents from the collection. In this article, we will discuss the syntax of find()
method and examples of how to retrieve all documents, specific documents based on certain criteria, and specific fields
In order to explain with examples, let’s create a student
collection and use this for all our examples below.
# Create Collection "Student"
db.student.insertMany([
{
_id: 1,
name: "Jimmy",
age: 24,
gender: "Male",
email: "[email protected]"
},
{
_id: 2,
name: "Elena",
age: 20,
gender: "Female",
email: "[email protected]"
},
{
_id: 3,
name: "Caroline",
age: 25,
gender: "Female",
email: "[email protected]"
},
{
_id: 4,
name: "Elan",
age: 23,
gender: "Male",
email: "[email protected]"
}
]
)
1. MongoDB find() Method Usage
To find the documents from the MongoDB collection, use the db.collection.find() method. This find() method returns a cursor to the documents that match the query
criteria. When you run this command from the shell or from the editor, it automatically iterates the cursor to display the first 20 documents. You can use this method with or without any arguments for criteria.
1.1 Syntax of MongoDB find() method
The basic syntax of the db.collection.find()
method in MongoDB is as follows.
# Syntax of find()
db.collection.find(expression, projection_fields)
Here,
expression
is an optional parameter that indicates the selection criteria for the documents to be retrieved. The collection’s documents will all be retrieved if no expression is provided.projection_fields
is an option that allows us to specify which fields should be included or excluded in the documents that will be fetched.options
parameter is also optional. The options parameter indicates the additional options for the query like skip and limit options.
1.2 Returns
Return
value of the find() method will be a cursor object, which can be iterated over to retrieve each document that matches the query criteria. The cursor is a pointer to the documents that are returned by the find()
method.
2. Find the Documents from MongoDB Collection
To retrieve all documents from the MongoDB collection, use the find() method without any arguments. By default, this method returns 20 documents from the collection. As mentioned in the beginning the find() method actually returns a cursor to the documents that match the query
criteria. When you run this command from the shell or from the editor, it automatically iterates the cursor to display the first 20 documents.
#Find all documents in the collection
db.student.find()
This query will return the entire document of the collection student
in the result. Since we have fewer than 2o documents, it returns all of them.
3. Find documents that match a specific condition
Specify a query parameter to the find()
operation in MongoDB to find documents matching specific criteria or conditions. For example, When a field’s value exceeds or equals (>=)
the specified value, documents with such values are selected using the $gte
operator. Here is an example.
#Usage of $gte operator with find() method
db.student.find({ age: { $gte: 23 } })
In the above example, we have a query in the find()
method where the age
field is specified with the $gte
operator to perform a greater-than-equal operation. The MongoDB find()
method locates all student collection records where the age
field equals or exceeds 23
. In the output, the documents are retrieved which satisfied the specific condition set in the find()
method.
4. Find documents that match multiple conditions
We can also specify multiple conditions in the find()
method by including multiple key-value pairs in the query parameter. The query of the MongoDB find()
method where multiple conditions are set is provided below.
#Usage of multiple key-value pairs
db.student.find({ age: { $gt: 23 }, gender: "Male" })
The output of this query returned the following documents where the age
is greater than 23
and the gender
is Male
.
5. Find documents with specific fields
Next, we can use the projection parameter to retrieve only specific fields from the retrieved documents instead of obtaining the entire MongoDB collection of data from the document. MongoDB Projection is a unique feature that enables you to choose only the information that is required.
Let’s consider the following query of the MongoDB find()
method that uses projection.
#Usage of projection parameter
db.student.find({}, { name: 1, age: 1 })
For example, here, the find()
method is implied over the student collection. Initially, the find()
method is set with empty curly braces {}
specifying no query filter, which in this case means that we want to retrieve all documents in the collection.
Then, the second set of curly braces in the find()
method specifies the projection, which is the set of fields to retrieve from each document. In this case, we want to retrieve only the name
and age
fields. The value of 1 against each field name indicates that we included only these fields in the result. Now, the output is fetched below where all the documents are retrieved with the specific fields.
To find arrays refer to find a document with an array that contains a specific value.
6. Find a few documents using the limit()
Sometimes, we may need to get only a few documents from the collection, MongoDB provides the limit()
method to accomplish this. The limit()
method takes the integer value to limit the number of documents. Following is the query where the limit()
method is used.
#Usage of limit() method
db.student.find().limit(2)
For example, we first used the find()
method to retrieve documents from a MongoDB collection student. Here, the find()
method is passed with an empty parameter which indicates that it will fetch all the documents in the result. But after the find()
method, we have called the limit()
method. The limit()
method is specified with the value 2
as an argument that returned only two documents from the collection. As a result, the output listed below returns the collection’s first two documents.
More details about the limit() function can be found here.
7. Conclusion
In conclusion, we have discussed the syntax of the db.collection.find()
method in MongoDB and provided some examples to demonstrate the usage by mastering the find()
operation. The find()
method allows us to query the database for specific documents based on certain criteria, no criteria. By default, it returns 20 records/documents from the collection. In this article, we will discuss the syntax of find()
method and examples of how to retrieve all documents, specific documents based on certain criteria, and specific fields
Related Articles
- MongoDB Find records where the array field is empty
- MongoDB Find Objects Between two Dates
- MongoDB find() with all ($all Operator)
- MongoDB find() with $where Operator
- Find Document from an Array that Contains a Specific value in MongoDB
- Get All Field Names in MongoDB
- Query Nested Objects in MongoDB
- MongoDB Search in Array of Objects
- MongoDB Query Case Insensitive
- MongoDB Query Using Dates
- MongoDB Query Dates by using ISODate
- MongoDB Select Single Field from Documents in a Collection
- MongoDB Query Where Array Size is Greater than 1
- MongoDB Query with “like” Example
- Retrieve Queried Element in an Object Array in MongoDB
- Using $ifNull Operator in MongoDB
- Search Object by its ObjectId in Mongo console