MongoDB Query to Find All Documents
Use
db.collection.find({}) to find all documents in a MongoDB collection; the empty curly braces {} mean no filter is applied.Examples
Inputdb.users.find({})
Output[{ "_id": 1, "name": "Alice" }, { "_id": 2, "name": "Bob" }]
Inputdb.products.find({})
Output[{ "_id": 101, "item": "Pen" }, { "_id": 102, "item": "Notebook" }]
Inputdb.emptyCollection.find({})
Output[]
How to Think About It
To find all documents, think of it like asking for every item in a box without any condition. In MongoDB, the
find method retrieves documents, and passing an empty object {} means no filtering, so it returns everything.Algorithm
1
Identify the collection you want to search.2
Use the <code>find</code> method on that collection.3
Pass an empty object <code>{}</code> as the filter to select all documents.4
Execute the query to get all documents.5
Return the list of documents found.Code
mongodb
const allDocuments = await db.collection('myCollection').find({}).toArray(); printjson(allDocuments);
Output
[
{ "_id": ObjectId("..."), "field1": "value1" },
{ "_id": ObjectId("..."), "field2": "value2" }
]
Dry Run
Let's trace finding all documents in the 'users' collection.
1
Call find with empty filter
db.users.find({}) means select all documents because filter is empty.
2
Retrieve documents
MongoDB returns all documents like [{_id:1, name:'Alice'}, {_id:2, name:'Bob'}].
| Step | Action |
|---|---|
| 1 | find({}) called with empty filter |
| 2 | All documents returned |
Why This Works
Step 1: Empty filter means no restriction
Passing {} to find tells MongoDB to not filter out any documents.
Step 2: find returns a cursor
find returns a cursor to iterate over all matching documents.
Step 3: Convert cursor to array
Using toArray() collects all documents into a list for easy use.
Alternative Approaches
Using find with projection
mongodb
db.collection.find({}, { _id: 0, name: 1 }).toArray();Returns all documents but only shows the 'name' field, hiding others.
Using findOne in a loop (legacy)
mongodb
let doc = db.collection.findOne({}); while(doc) { printjson(doc); doc = db.collection.findOne({_id: {$gt: doc._id}}); }
Less efficient and more complex; not recommended for simply getting all documents.
Complexity: O(n) time, O(n) space
Time Complexity
The query scans all documents in the collection, so time grows linearly with the number of documents.
Space Complexity
Storing all documents in memory requires space proportional to the number of documents returned.
Which Approach is Fastest?
Using find({}) with no projection is fastest for retrieving all documents; adding projections or looping manually adds overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| find({}) | O(n) | O(n) | Getting all documents quickly |
| find({}, projection) | O(n) | O(n) | Getting specific fields only |
| findOne loop | O(n^2) | O(1) | Legacy, inefficient for all documents |
Use
find({}) with an empty filter to quickly get all documents in a collection.Forgetting to use empty curly braces
{} and passing null or no argument, which may cause errors or unexpected results.