MongoDB mongosh Find
MongoDB mongosh – Find Documents 🔍
It works like SELECT in SQL but uses JSON-style queries.
1️⃣ Find All Documents
Readable format:
2️⃣ Find with Condition
Multiple conditions:
3️⃣ Find One Document
📌 Returns the first matching document only.
4️⃣ Projection (Select Specific Fields)
Include fields:
Exclude _id:
⚠️ You cannot mix 1 and 0 (except _id).
5️⃣ Comparison Operators
| Operator | Meaning |
|---|---|
$eq | equal |
$ne | not equal |
$gt | greater than |
$gte | ≥ |
$lt | less than |
$lte | ≤ |
$in | in list |
$nin | not in list |
6️⃣ Logical Operators
$and
$or
$not
7️⃣ Find in Arrays
Using $all:
8️⃣ Regular Expression (Search)
9️⃣ Sorting, Limit & Skip
Sort (Ascending = 1, Descending = -1)
Limit
Skip
🔟 Count Documents
SQL vs MongoDB (Find)
SQL
MongoDB
