In MongoDB, the find() and findOne()
methods are used to retrieve documents from a collection.
You can display all documents, search by a specific field,
use multiple conditions, and retrieve only the information
you need.
In this lesson, we will practise document retrieval using a student management database.
Select the database:
use school
Insert some sample student records:
db.students.insertMany([
{
name: "Rahul",
age: 20,
course: "ADCA",
city: "Patna"
},
{
name: "Amit",
age: 22,
course: "Python",
city: "Gaya"
},
{
name: "Priya",
age: 19,
course: "Web Development",
city: "Patna"
},
{
name: "Neha",
age: 21,
course: "Python",
city: "Aurangabad"
},
{
name: "Ravi",
age: 23,
course: "ADCA",
city: "Gaya"
}
])
Note: Run this insertion example only once if you do not want duplicate sample records.
The find() method without a filter retrieves
all documents in a collection.
db.students.find()
To display the results in a more readable format in MongoDB Shell, use:
db.students.find().pretty()
Explanation: MongoDB returns all student
documents stored in the students collection.
You can pass a filter object to find()
to retrieve documents matching a particular value.
db.students.find({
name: "Rahul"
})
db.students.find({
course: "Python"
})
db.students.find({
city: "Patna"
})
MongoDB returns every document that matches the supplied filter. If several students have the same course or city, all matching documents are returned.
The findOne() method returns one matching
document. If no document matches, it returns
null.
db.students.findOne({
name: "Rahul"
})
db.students.findOne({
course: "Python"
})
If multiple documents match, findOne()
returns one of them. Without a sort, you should not rely
on which matching document it returns.
You can search for documents using numeric fields such as age, marks, fees, and salary.
db.students.find({
age: 20
})
db.students.find({
age: 21
})
db.students.find({
city: "Gaya",
age: 22
})
When multiple fields are provided in the same filter, MongoDB requires all those field conditions to match.
By default, conditions placed together in a filter use AND logic.
db.students.find({
course: "Python",
city: "Gaya"
})
The document must have both the specified course and city to match.
db.students.find({
age: 20,
city: "Patna"
})
db.students.find({
name: "Neha",
course: "Python"
})
Comparison operators let you find documents using conditions such as greater than, less than, or equal to.
db.students.find({
age: { $gt: 20 }
})
db.students.find({
age: { $lt: 22 }
})
db.students.find({
age: { $gte: 21 }
})
db.students.find({
age: { $ne: 20 }
})
These operators are useful for searching student marks, employee salaries, product prices, and other numeric data.
Projection allows you to select which fields should appear in the query results.
db.students.find(
{},
{ name: 1, course: 1 }
)
The _id field is included by default unless
you explicitly exclude it.
db.students.find(
{},
{ _id: 0, name: 1, course: 1 }
)
db.students.find(
{},
{ _id: 0, name: 1 }
)
Projection changes the fields displayed in the results; it does not delete other fields from stored documents.
Combine find() with sort()
and limit() to control the order and
number of returned documents.
db.students.find().sort({
age: 1
})
db.students.find().sort({
age: -1
})
db.students.find().limit(2)
db.students.find({
course: "Python"
}).sort({
age: 1
})
You can use findOne() to check whether
a document matching a filter exists.
db.students.findOne({
name: "Rahul"
})
If a matching document exists, MongoDB returns it.
Otherwise, the result is null.
To count how many documents match a filter, use
countDocuments().
db.students.countDocuments({
course: "Python"
})
| Requirement | Command |
|---|---|
| Find all documents | db.students.find() |
| Find one document | db.students.findOne() |
| Find by name | db.students.find({ name: "Rahul" }) |
| Find by course | db.students.find({ course: "Python" }) |
| Age greater than 20 | db.students.find({ age: { $gt: 20 } }) |
| Show selected fields | db.students.find({}, { name: 1, course: 1 }) |
| Sort by age | db.students.find().sort({ age: 1 }) |
| Limit results | db.students.find().limit(2) |
| Count Python students | db.students.countDocuments({ course: "Python" }) |
Use the school database and the
students collection created in this lesson.
Write MongoDB commands to complete these tasks.
1. Which method retrieves all matching documents?
2. Which operator means greater than?
3. What does limit(2) do?