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):

JavaScript
  • 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:

JSON

3. Hands-on Examples – Start Simple, Go Deeper

Example 1: Find ALL documents (no filter)

JavaScript

→ Returns a cursor with all documents (shows first 20 by default in mongosh, type it to see next batch).

Better (human-readable):

JavaScript

Example 2: findOne() — Get just one document (very common)

JavaScript

→ Returns first matching document (single object, not cursor)

JSON

Example 3: Equality filter (exact match)

JavaScript

→ All wishes sent to Priya

Example 4: Comparison operators ($gt, $gte, $lt, $lte, $ne, $in, $nin)

JavaScript
JavaScript

Example 5: Nested / Dot notation (for embedded fields)

If you had:

JavaScript
JavaScript

Example 6: Array contains element

JavaScript

Example 7: Projection — Show only certain fields (saves bandwidth)

JavaScript
  • 1 = include field
  • 0 = exclude field
  • _id is included by default — you must explicitly exclude it if unwanted

Example 8: Sorting + Limiting + Skipping (Pagination!)

JavaScript

Order matters: Always .sort() → .skip() → .limit()

Example 9: Count matches (without fetching all data)

JavaScript

→ 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!

  1. use valentines2026
  2. Find all wishes with rating >= 10 → .pretty()
  3. Find wishes sent to you (change “Rahul” to your name if added)
  4. Show only from and message for high-rated ones
  5. 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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *