✅ Node.js MongoDB – Find Documents
MongoDB provides methods to retrieve data from a collection:
⭐ 1. Find One Document – findOne()
Example
⭐ 2. Find All Documents – find()
Example
⭐ 3. Find With Condition (WHERE query)
Example
⭐ 4. Find With Multiple Conditions
Example
⭐ 5. Find With Comparison Operators
| MongoDB Operator | Meaning |
|---|
$gt | greater than |
$lt | less than |
$gte | greater than or equal |
$lte | less than or equal |
$ne | not equal |
$in | in array |
Example
⭐ 6. Find With Sorting + Limit
🎉 Summary
| Method | Description |
|---|
findOne(filter) | Returns 1 matching document |
find(filter) | Returns all matching documents (cursor) |
sort() | Sort results |
limit() | Limit the number of results |
toArray() | Convert cursor to array |