MongoDB Query API
MongoDB Query API 📘
It is powerful, flexible, and easy to use compared to traditional SQL queries.
1️⃣ Basic Query Structure
-
query → filter condition
-
projection → fields to include or exclude
2️⃣ Read Operations (Find)
Find all documents
Find with condition
Find one document
3️⃣ Projection (Select Specific Fields)
Include fields:
Exclude field:
4️⃣ Comparison Query Operators
| Operator | Meaning |
|---|---|
$eq |
equal |
$ne |
not equal |
$gt |
greater than |
$gte |
greater than or equal |
$lt |
less than |
$lte |
less than or equal |
$in |
in array |
$nin |
not in array |
Example:
5️⃣ Logical Query Operators
$and
$or
$not
6️⃣ Element Query Operators
$exists
$type
7️⃣ Array Query Operators
$all
$size
8️⃣ Update Operations
Update one
Update many
Increment value
9️⃣ Delete Operations
Delete one
Delete many
🔟 Sorting, Limiting & Skipping
Sort
Limit
Skip
1️⃣1️⃣ Regular Expression (Search)
1️⃣2️⃣ Count Documents
SQL vs MongoDB Query Example
SQL
MongoDB
🔑 Summary
-
MongoDB Query API uses JSON-like syntax
-
Supports rich operators
-
Faster and flexible than SQL
-
Ideal for modern applications
