Chapter 7: MongoDB mongosh Find
MongoDB mongosh Find (aka the most used command after insert — Read / Retrieve data using find() and friends)
This is the R in CRUD — the heart of almost every app. find() is how you ask MongoDB: “Show me the documents that match these conditions.”
1. What is db.collection.find() Really?
- Purpose: Selects documents from a collection (or view) and returns a cursor (a pointer to the results — not all data at once).
- Returns all matching documents (or a subset if you limit/sort/project).
- If no filter → returns everything in the collection.
- Very flexible — supports equality, comparisons, arrays, nested fields, regex, logical operators ($and/$or), geospatial, text search, and more.
Basic syntax in mongosh (2026 style):
|
0 1 2 3 4 5 6 7 8 9 |
db.collection.find( <query>, // filter conditions (optional) <projection> // which fields to return (optional) ) |
- You can chain helper methods after it: .pretty(), .sort(), .limit(), .skip(), .count(), etc.
There is also findOne() — returns only the first matching document (no cursor, just one object).
2. Let’s Use Our Previous Data (valentines2026 db)
Assume we have this in db.wishes from earlier inserts:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
[ { "_id": ObjectId("..."), "from": "Rahul", "to": "Priya", "message": "Happy Valentine's Day! ... ❤️", "date": ISODate("2026-02-14..."), "rating": 10 }, { "from": "Amit", "to": "Sneha", "message": "You make every aggregation feel simple!", "giftIdea": "Biryani date" }, { "from": "Priya", "to": "Rahul", "message": "Thanks for the MongoDB lessons...", "rating": 11 }, { "from": "Class2026", "to": "Everyone", "message": "Keep querying with love!" } ] |
3. Hands-on Examples – Start Simple, Go Deeper
Example 1: Find ALL documents (no filter)
|
0 1 2 3 4 5 6 |
db.wishes.find() |
→ Returns a cursor with all documents (shows first 20 by default in mongosh, type it to see next batch).
Better (human-readable):
|
0 1 2 3 4 5 6 |
db.wishes.find().pretty() |
Example 2: findOne() — Get just one document (very common)
|
0 1 2 3 4 5 6 |
db.wishes.findOne({ from: "Rahul" }) |
→ Returns first matching document (single object, not cursor)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "_id": ObjectId("..."), "from": "Rahul", "to": "Priya", "message": "...", "date": ISODate("..."), "rating": 10 } |
Example 3: Equality filter (exact match)
|
0 1 2 3 4 5 6 |
db.wishes.find({ to: "Priya" }).pretty() |
→ All wishes sent to Priya
Example 4: Comparison operators ($gt, $gte, $lt, $lte, $ne, $in, $nin)
|
0 1 2 3 4 5 6 7 8 9 10 |
// Rating 10 or higher db.wishes.find({ rating: { $gte: 10 } }).pretty() // Not sent to Priya db.wishes.find({ to: { $ne: "Priya" } }) |
|
0 1 2 3 4 5 6 7 |
// Sent to Priya OR Sneha (using $in) db.wishes.find({ to: { $in: ["Priya", "Sneha"] } }) |
Example 5: Nested / Dot notation (for embedded fields)
If you had:
|
0 1 2 3 4 5 6 |
{ "sender": { "name": "Rahul", "city": "Hyderabad" }, ... } |
|
0 1 2 3 4 5 6 |
db.wishes.find({ "sender.city": "Hyderabad" }) |
Example 6: Array contains element
|
0 1 2 3 4 5 6 7 8 |
// If you add hobbies later: hobbies: ["coding", "biryani"] db.wishes.find({ hobbies: "biryani" }) // contains "biryani" db.wishes.find({ hobbies: { $all: ["coding", "biryani"] } }) // must have both |
Example 7: Projection — Show only certain fields (saves bandwidth)
|
0 1 2 3 4 5 6 7 8 9 10 |
// Only show from, to, message — hide _id db.wishes.find( { rating: { $gte: 10 } }, { from: 1, to: 1, message: 1, _id: 0 } ).pretty() |
- 1 = include field
- 0 = exclude field
- _id is included by default — you must explicitly exclude it if unwanted
Example 8: Sorting + Limiting + Skipping (Pagination!)
|
0 1 2 3 4 5 6 7 8 9 10 |
// Highest rating first, show only top 2 db.wishes.find().sort({ rating: -1 }).limit(2).pretty() // Skip first 1, show next 2 (page 2 if page size=2) db.wishes.find().sort({ date: -1 }).skip(1).limit(2).pretty() |
Order matters: Always .sort() → .skip() → .limit()
Example 9: Count matches (without fetching all data)
|
0 1 2 3 4 5 6 |
db.wishes.find({ to: "Priya" }).count() |
→ Just a number (fast!)
4. Quick Reference Table – Most Used Patterns
| What you want | Command Example |
|---|---|
| All documents | db.wishes.find().pretty() |
| One document | db.wishes.findOne({ from: “Rahul” }) |
| Exact match | { city: “Hyderabad” } |
| Greater / less | { rating: { $gt: 9 } } |
| In list | { to: { $in: [“Priya”, “Sneha”] } } |
| Array contains | { hobbies: “cricket” } |
| Only some fields | find(…, { message: 1, _id: 0 }) |
| Sort descending | .sort({ date: -1 }) |
| Top 5 | .sort({ rating: -1 }).limit(5) |
| Pagination (page 3, 10 per page) | .sort({ date: -1 }).skip(20).limit(10) |
| Count only | .find({…}).count() |
5. Mini Exercise – Try Right Now!
- use valentines2026
- Find all wishes with rating >= 10 → .pretty()
- Find wishes sent to you (change “Rahul” to your name if added)
- Show only from and message for high-rated ones
- Sort by date newest first → limit to 3
Understood beta? This is where MongoDB starts feeling magical — you ask, it answers instantly.
Next class options:
- Update — change messages, add hearts?
- Delete — remove old wishes?
- Complex queries: $and, $or, $regex, $elemMatch?
- Or aggregation pipeline intro (group by sender, count wishes per person)?
Tell me — we’re building something beautiful here! 🚀❤️
Any confusion in find()? Ask anything — no silly questions on Valentine’s Day! 😄
