← Back to MongoDB Course | Chapter 3: CRUD — Read | Lesson 1 of 7

find()

find returns all documents in a collection, or those that match a filter.

In this page:

  1. find()
Syntax
javascript
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()

bash
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
  1. Forgetting find returns a cursor, not an array
  2. Running find on huge collections without a filter or limit
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.