MongoDB Find

Query documents with find and findOne.

✍️ 2 exercises in this lesson
  • db.students.find() returns all documents.
  • db.students.find({ city: "Lahore" }) filters by field values.
  • findOne() returns only the first match.
  • countDocuments(filter) counts matches.

Key Takeaways: MongoDB Find

Mastering MongoDB Find 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 Find Code Example

MongoDB
db.students.find({ city: "Karachi" })
db.students.findOne({ name: "Ali" })
db.students.countDocuments()
Output
[ { _id: ObjectId('...'), name: 'Sara', city: 'Karachi', marks: 94 } ]
{ _id: ObjectId('...'), name: 'Ali', city: 'Lahore', marks: 81 }
3
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

MongoDB Find Practice Exercises

Check what you learned in “MongoDB Find”.

/ 2 solved
Fill in the blank

Return only the first matching document.

db.students.({ city: "Lahore" })
✓ Correct! Not quite. Try again.
Multiple choice

Which method counts the documents that match a filter?

✓ 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