0
0
MongodbHow-ToBeginner · 3 min read

How to Find Documents in MongoDB: Syntax and Examples

To find documents in MongoDB, use the find() method on a collection. You can pass a query object to specify criteria, or leave it empty to get all documents.
📐

Syntax

The find() method is used on a MongoDB collection to retrieve documents. It accepts a query object to filter results and returns a cursor to iterate over matching documents.

  • db.collection.find(query, projection)
  • query: An object specifying search criteria (empty object {} means no filter).
  • projection: Optional object to specify which fields to include or exclude.
mongodb
db.collection.find(query, projection)
💻

Example

This example shows how to find all documents in the users collection and then find documents where the age is greater than 25.

mongodb
use testdb

// Insert sample documents
db.users.insertMany([
  { name: "Alice", age: 30 },
  { name: "Bob", age: 22 },
  { name: "Charlie", age: 28 }
])

// Find all documents
const allUsers = db.users.find({}).toArray()
printjson(allUsers)

// Find users with age > 25
const olderUsers = db.users.find({ age: { $gt: 25 } }).toArray()
printjson(olderUsers)
Output
[ { "_id": ObjectId("..."), "name": "Alice", "age": 30 }, { "_id": ObjectId("..."), "name": "Bob", "age": 22 }, { "_id": ObjectId("..."), "name": "Charlie", "age": 28 } ] [ { "_id": ObjectId("..."), "name": "Alice", "age": 30 }, { "_id": ObjectId("..."), "name": "Charlie", "age": 28 } ]
⚠️

Common Pitfalls

Common mistakes when using find() include:

  • Forgetting to convert the cursor to an array or iterate it, so no results appear.
  • Using incorrect query syntax, like missing curly braces or wrong operators.
  • Expecting find() to return a single document instead of a cursor (use findOne() for one document).
mongodb
// Wrong: expecting find() to return a document directly
const user = db.users.find({ name: "Alice" })
printjson(user) // prints a cursor object, not the document

// Right: convert cursor to array or use findOne()
const userArray = db.users.find({ name: "Alice" }).toArray()
printjson(userArray)

const singleUser = db.users.findOne({ name: "Alice" })
printjson(singleUser)
Output
Cursor object printed (not useful) [ { "_id": ObjectId("..."), "name": "Alice", "age": 30 } ] { "_id": ObjectId("..."), "name": "Alice", "age": 30 }
📊

Quick Reference

CommandDescription
db.collection.find({})Find all documents in the collection
db.collection.find({ field: value })Find documents matching the field value
db.collection.find({ age: { $gt: 25 } })Find documents where age is greater than 25
db.collection.findOne({ field: value })Find a single document matching the query
cursor.toArray()Convert the cursor to an array of documents

Key Takeaways

Use db.collection.find(query) to search documents with optional filters.
find() returns a cursor; use toArray() or iterate to access documents.
Use findOne() to get a single document instead of a cursor.
Always check query syntax and operators to avoid empty results.
An empty query object {} returns all documents in the collection.