- Projection (second argument of find) picks fields:
{ name: 1, _id: 0 } .sort({ marks: -1 }): -1 for descending, 1 for ascending..limit(n)and.skip(n)for paging.
MongoDB Projection, Sort & Limit
Choose fields and order results.
✍️ 2 exercises in this lessonKey Takeaways: MongoDB Projection, Sort & Limit
Mastering MongoDB Projection, Sort & Limit helps you write clean and reliable MongoDB code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
MongoDB Projection, Sort & Limit Code Example
MongoDBdb.students.find({}, { name: 1, marks: 1, _id: 0 })
.sort({ marks: -1 })
.limit(2)
Output
[ { name: 'Sara', marks: 94 }, { name: 'Ali', marks: 81 } ]
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
MongoDB Projection, Sort & Limit Practice Exercises
Check what you learned in “MongoDB Projection, Sort & Limit”.
/ 2 solved
Fill in the blank
Sort by marks, highest first.
.sort({ marks: })
✓ Correct!
Not quite. Try again.
Multiple choice
In the projection { name: 1, _id: 0 }, what does _id: 0 do?
✓ Correct!
Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services