1. Pick fields
The second find() argument is a projection: 1 includes a field, 0 excludes it; _id is included unless you set it to 0.
{ name: 1, _id: 0 }Typorithm · medium
Step 1 of 8 · intro card
Query Operators
Shape results after filtering: a projection picks which fields to return, .sort orders them, and .skip + .limit page through the set.
A projection is the second argument to find(): set fields to 1 to include or 0 to exclude; _id is returned unless you set it to 0.
.sort orders the cursor by one or more fields, with 1 ascending and -1 descending.
.skip(N).limit(M) implements pagination — skip past earlier pages and return only M documents.
Projection 1 includes a field, 0 excludes it.
.sort({ field: -1 }) sorts descending, 1 ascending.
.skip + .limit page through ordered results.
db.users.find({}, { name: 1, _id: 0 })
db.users.find({}).sort({ age: -1 })
db.users.find({}).sort({ age: -1 }).limit(10)
db.users.find({}).sort({ age: -1 }).skip(20).limit(10)Connect each part of the code to the concept before you type.
The second find() argument is a projection: 1 includes a field, 0 excludes it; _id is included unless you set it to 0.
{ name: 1, _id: 0 }.sort orders documents: -1 is descending, 1 is ascending.
.sort({ age: -1 }).limit caps how many documents the cursor returns.
.limit(10).skip jumps past the first N documents, paired with limit for paging.
.skip(20)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.