All academies
Roadmap
LEVEL 3 · CRUD OPERATIONS

Reading with find()

12 min read

find(filter, projection) is the workhorse of MongoDB. The filter selects documents, the projection selects fields.

mongosh
db.products.find({ category: "electronics" })

// only name and price, hide _id
db.products.find(
  { category: "electronics" },
  { name: 1, price: 1, _id: 0 }
)

Cursor methods

find() returns a cursor you can chain: .sort(), .limit(), .skip(), .count(). Sort takes 1 for ascending and -1 for descending.

mongosh
db.products.find()
  .sort({ price: -1 })   // most expensive first
  .skip(5)
  .limit(5)              // page 2 of 5-per-page

findOne and dot notation

findOne() returns a single document (or null). Query nested fields with dot notation — always quote the path:

mongosh
db.users.findOne({ "address.city": "Mumbai" })

Try it yourself

db.products.find({ category: "electronics" }).sort({ price: -1 }).limit(3)
Run in Shell

Check your understanding

Q1.

In a projection, what does { name: 1, _id: 0 } mean?

Q2.

The collection has 12 products. What does this return?

code
db.products.find().skip(10).limit(5).count()
Q3.

How do you query the nested field city inside address?