The find() method retrieves documents from a MongoDB collection, offering query filters, projection to include or exclude fields, and parameters to customize results while benefiting from automatic indexing for efficient performance.
- Query Operators: Supports comparison, logical, and element operators for complex queries.
- Field Selection: Allows specifying which fields to include or exclude, reducing data transfer overhead.
- Automatic Indexing: Leverages indexes to optimize query performance and improve speed.
Syntax:
db.Collection_name.find(selection_criteria, projection,options)
In the above syntax:
- db.Collection_name.find: Specifies the database (db) and the collection (Collection_name) from which to retrieve documents.
- selection_criteria (document): Defines which documents to find based on specific conditions. An empty document ({}) retrieves all documents.
- projection (document): Specifies which fields to include or exclude from the returned documents.
- options (document): Includes additional parameters like sorting, limiting, and skipping results.
Examples of Find() Method
To understand find()
method in MongoDB we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called student of gfg database which contains the following documents:
- Database: gfg
- Collections: student
- Document: Three documents contains the details of the students
Example 1: Find All Documents in a Collection
Let's Find all student records from the "student" collection where the student's age is exactly 18.
db.student.find()
Output:
Explanation: In the above query, we have found all the documents in the students collections in MongoDB.
Example 2: Find Documents with a Specific Condition
Find all the students whose age is exactly 18:
db.student.find({age:18})
Output:
Explanation: In the above query, we have find those students whose age is 18.
Example 3: Using Nested Documents in Queries
Let's Find student records from the "student" collection where the student's math score is 230 and science score is 234.
db.student.find({score:{math: 230, science: 234}})
Output:
Explanation:
In the above MongoDB query, we have searches for student records in the "student" collection based on their exam performance in two subjects. It retrieves documents where the student's math score is exactly 230 and their science score is exactly 234. The query uses a nested document to specify these conditions within the "score" field of each student record.
Example 4: Using Projection
Retrieve the names of all students while excluding the score
field:
db.student.find({}, { name: 1, score: 0 })
Output:
[
{ "name": "Aman", "age": 18 },
{ "name": "Suraj", "age": 21 },
{ "name": "Joyita", "age": 18 }
]
Explanation: The projection includes only the name
field while explicitly excluding the score
field.
Example 5: Sorting Results
Retrieve all student records sorted by age in ascending order:
db.student.find().sort({ age: 1 })
Output:
[
{ "name": "Aman", "age": 18, "score": { "math": 230, "science": 234 } },
{ "name": "Joyita", "age": 18, "score": { "math": 230, "science": 234 } },
{ "name": "Suraj", "age": 21, "score": { "math": 250, "science": 200 } }
]
Explanation: The projection includes only the name
field while explicitly excluding the score
field.
Example 6: Limiting Results
Retrieve only the first two student records:
db.student.find().limit(2)
Output:
[
{ "name": "Aman", "age": 18, "score": { "math": 230, "science": 234 } },
{ "name": "Suraj", "age": 21, "score": { "math": 250, "science": 200 } }
]
Explanation: The limit
method restricts the number of documents returned to the specified value (2
).
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators