find()
find returns all documents in a collection, or those that match a filter.
In this page:
Syntax
db.collection_name.find(filter, projection)
db.collection_name.find({ field: value })
find()
db.collection.find(filter, projection) returns a cursor over matching documents. An empty or missing filter matches everything. In mongosh the first 20 results print automatically, and you can type it to see more.
Note:
find with no arguments returns everything, so add a filter on large collections.
Example: find()
test> db.users.insertMany([
... { _id: 1, name: "Ada", age: 36, city: "London" },
... { _id: 2, name: "Bob", age: 25, city: "Oslo" },
... { _id: 3, name: "Cy", age: 41, city: "Oslo" }
... ])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.users.find()
[
{ _id: 1, name: 'Ada', age: 36, city: 'London' },
{ _id: 2, name: 'Bob', age: 25, city: 'Oslo' },
{ _id: 3, name: 'Cy', age: 41, city: 'Oslo' }
]
test> db.users.find({ city: "Oslo" })
[
{ _id: 2, name: 'Bob', age: 25, city: 'Oslo' },
{ _id: 3, name: 'Cy', age: 41, city: 'Oslo' }
]
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Forgetting find returns a cursor, not an array
- Running find on huge collections without a filter or limit
- Misspelling the collection name and getting an empty result
Chapter Summary
- find(filter, projection) returns a cursor
- Empty filter matches all
- mongosh prints the first 20
- Add filters and limits on big data
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: