Projection, Sort, and Limit
Project fields
Pass a second argument to include (1) or exclude (0) fields. Do not mix inclusion and exclusion except for _id.
db.books.find(
{ year: { $gte: 2024 } },
{ title: 1, year: 1, _id: 0 }
)
Sort
1 is ascending, -1 is descending.
db.books.find().sort({ year: -1, title: 1 })
Limit and skip
Use for simple paging. Deep skip values get expensive on large collections—prefer range queries later.
db.books.find().sort({ year: -1 }).skip(10).limit(10)
Count documents
Prefer countDocuments with a filter when you need an accurate count.
db.books.countDocuments({ tags: "nosql" })
Comments
One comment per signed-in account. Comments are saved with this page’s URL.