⬅ Previous Topic
Delete Operations in MongoDBNext Topic ⮕
MongoDB Query Operators Overview⬅ Previous Topic
Delete Operations in MongoDBNext Topic ⮕
MongoDB Query Operators OverviewIn MongoDB, when we retrieve documents using the find()
method, we often don't need all the fields. This is where projection helps — it allows us to specify which fields to include or exclude in the query results.
We also use sorting to arrange the documents in ascending or descending order based on a specific field, such as name, age, or date.
Projection means selecting only specific fields to be returned in the output of a query. By default, MongoDB returns all fields in a document.
Let’s first insert a few sample documents:
db.students.insertMany([
{ name: "Alice", age: 22, course: "Math", grade: "A" },
{ name: "Bob", age: 23, course: "Physics", grade: "B" },
{ name: "Charlie", age: 21, course: "Chemistry", grade: "A" }
])
Now, let’s retrieve only the name
and course
fields from each document:
db.students.find({}, { name: 1, course: 1, _id: 0 })
Output:
{ "name": "Alice", "course": "Math" } { "name": "Bob", "course": "Physics" } { "name": "Charlie", "course": "Chemistry" }
Explanation: The first argument {}
matches all documents. The second argument { name: 1, course: 1 }
tells MongoDB to include only the name
and course
fields. We explicitly exclude the _id
field using _id: 0
.
Q: What happens if you try to include and exclude fields together?
A: You can't mix inclusion and exclusion in the same projection (except for the _id
field). MongoDB will throw an error.
If you want to exclude fields instead of including them, just set them to 0
in the projection:
db.students.find({}, { grade: 0, _id: 0 })
Output:
{ "name": "Alice", "age": 22, "course": "Math" } { "name": "Bob", "age": 23, "course": "Physics" } { "name": "Charlie", "age": 21, "course": "Chemistry" }
Explanation: All fields are returned except grade
and _id
.
Sorting allows you to order the query result by one or more fields.
1
for ascending order-1
for descending order
db.students.find({}, { _id: 0 }).sort({ name: 1 })
Output:
{ "name": "Alice", "age": 22, "course": "Math", "grade": "A" } { "name": "Bob", "age": 23, "course": "Physics", "grade": "B" } { "name": "Charlie", "age": 21, "course": "Chemistry", "grade": "A" }
Explanation: The documents are sorted alphabetically by the name
field.
db.students.find({}, { _id: 0 }).sort({ age: -1 })
Output:
{ "name": "Bob", "age": 23, "course": "Physics", "grade": "B" } { "name": "Alice", "age": 22, "course": "Math", "grade": "A" } { "name": "Charlie", "age": 21, "course": "Chemistry", "grade": "A" }
Explanation: The documents are sorted by age in descending order.
Let’s sort first by grade
(ascending), then by age
(descending):
db.students.find({}, { _id: 0 }).sort({ grade: 1, age: -1 })
Output:
{ "name": "Alice", "age": 22, "course": "Math", "grade": "A" } { "name": "Charlie", "age": 21, "course": "Chemistry", "grade": "A" } { "name": "Bob", "age": 23, "course": "Physics", "grade": "B" }
Explanation: Documents are sorted first by grade alphabetically (A before B), and within the same grade, they are sorted by age in descending order.
projection
to include or exclude specific fields in the resultsort()
to order the result by one or more fields1
for ascending and -1
for descending orderNow that you understand projection and sorting, you’re ready to dive deeper into advanced querying with MongoDB operators like $gt
, $in
, and $regex
.
⬅ Previous Topic
Delete Operations in MongoDBNext Topic ⮕
MongoDB Query Operators OverviewYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.