Querying: operators, projection, sort and limit
Harry
· 14 Sep 2026
· 2 views
Advertisement
Comparison operators
Match on ranges and sets using operators prefixed with $:
db.products.find({ price: { $gt: 1000 } }) // price > 1000
db.products.find({ price: { $gte: 500, $lte: 2000 } })// between 500 and 2000
db.products.find({ category: { $in: ["phones", "tablets"] } })
$gt,$gte,$lt,$lte– greater/less than.$ne– not equal;$in/$nin– in / not in a list.
Logical and pattern matching
db.users.find({ $or: [ { city: "Pune" }, { age: { $lt: 30 } } ] })
db.users.find({ name: { $regex: "^A", $options: "i" } }) // starts with A
Projection: return only what you need
The second argument to find chooses fields. 1 includes, 0 excludes:
db.users.find({ city: "Pune" }, { name: 1, age: 1, _id: 0 })
This returns just name and age, dropping _id – smaller payloads and clearer results.
Sort, skip and limit (pagination)
db.products.find()
.sort({ price: -1 }) // -1 descending, 1 ascending
.skip(20)
.limit(10)
sort orders results, then skip and limit return one page – here items 21–30 by price, highest first.
Key points
- Use
$gt/$lt/$in/$nefor ranges and sets,$orfor alternatives,$regexfor patterns. - Projection (
{ field: 1 }) returns only the fields you ask for. sort+skip+limitimplements ordering and pagination.