find()

Queries a collection and returns a cursor of matching documents. Accepts a filter document and optional projection.

Syntax

mongodb
db.collection.find(filter, projection)

Example

mongodb
// Find all active users:
db.users.find({ active: true })

// With projection (include/exclude fields):
db.users.find(
  { role: "admin" },
  { name: 1, email: 1, _id: 0 }
)

// With sort and limit:
db.users.find({}).sort({ name: 1 }).limit(10)