MongoDB Find
Query and retrieve documents from collections
🔍 Finding Documents
Finding documents in MongoDB means querying collections to retrieve data. Use find() to get multiple documents or findOne() to retrieve a single document matching your search criteria efficiently.
// Find all documents
db.users.find()
// Find one document
db.users.findOne({ name: "Alice" })
// Find with conditions
db.users.find({ age: { $gt: 25 } })
Find Methods
MongoDB provides powerful query methods to search and retrieve documents. You can find all documents, find specific ones, or use advanced filters to get exactly what you need.
find()
Retrieve all matching documents
db.users.find()
findOne()
Get first matching document
db.users.findOne({...})
Query Filters
Search with specific conditions
{ age: { $gt: 25 } }
Projection
Select specific fields to return
{ name: 1, age: 1 }
🔹 Find All Documents
Use find() without parameters to retrieve all documents in a collection:
// Find all users
db.users.find()
// Find all and format output
db.users.find().pretty()
Output:
{ _id: 1, name: "Alice", age: 25 }
{ _id: 2, name: "Bob", age: 30 }
{ _id: 3, name: "Carol", age: 28 }
🔹 Find One Document
Use findOne() to retrieve the first document that matches your query:
// Find first user
db.users.findOne()
// Find specific user by name
db.users.findOne({ name: "Alice" })
// Find by _id
db.users.findOne({ _id: 1 })
Output:
{ _id: 1, name: "Alice", age: 25, city: "New York" }
🔹 Find with Query Filters
Add conditions to find specific documents:
// Find users older than 25
db.users.find({ age: { $gt: 25 } })
// Find users from specific city
db.users.find({ city: "New York" })
// Find with multiple conditions
db.users.find({
age: { $gte: 25 },
city: "Boston"
})
Output:
{ _id: 2, name: "Bob", age: 30, city: "Boston" }
{ _id: 3, name: "Carol", age: 28, city: "Boston" }
🔹 Common Query Operators
MongoDB provides operators for advanced queries:
Comparison Operators:
- $eq: Equal to
- $ne: Not equal to
- $gt: Greater than
- $gte: Greater than or equal
- $lt: Less than
- $lte: Less than or equal
- $in: Matches any value in array
// Greater than
db.products.find({ price: { $gt: 100 } })
// In array
db.users.find({ city: { $in: ["Boston", "New York"] } })
// Not equal
db.orders.find({ status: { $ne: "cancelled" } })
🔹 Logical Operators
Combine multiple conditions with logical operators:
// AND condition (implicit)
db.users.find({
age: { $gt: 25 },
city: "Boston"
})
// OR condition
db.users.find({
$or: [
{ age: { $lt: 25 } },
{ city: "New York" }
]
})
// NOT condition
db.products.find({
price: { $not: { $gt: 100 } }
})
Output:
{ _id: 1, name: "Alice", age: 22, city: "New York" }
{ _id: 4, name: "David", age: 35, city: "New York" }
🔹 Projection (Select Fields)
Choose which fields to include or exclude in results:
// Include only name and age (exclude _id)
db.users.find(
{},
{ name: 1, age: 1, _id: 0 }
)
// Exclude specific fields
db.users.find(
{},
{ password: 0, email: 0 }
)
// Find with filter and projection
db.users.find(
{ age: { $gt: 25 } },
{ name: 1, city: 1 }
)
Output:
{ name: "Bob", age: 30 }
{ name: "Carol", age: 28 }
🔹 Sorting and Limiting
Sort results and limit the number of documents returned:
// Sort by age (ascending)
db.users.find().sort({ age: 1 })
// Sort by age (descending)
db.users.find().sort({ age: -1 })
// Limit results to 5 documents
db.users.find().limit(5)
// Skip first 10 and get next 5
db.users.find().skip(10).limit(5)
// Combine: sort, skip, limit
db.products.find()
.sort({ price: -1 })
.skip(0)
.limit(10)
Output:
{ _id: 2, name: "Bob", age: 30 }
{ _id: 3, name: "Carol", age: 28 }
{ _id: 1, name: "Alice", age: 25 }
🔹 Count Documents
Count how many documents match your query:
// Count all documents
db.users.countDocuments()
// Count with filter
db.users.countDocuments({ age: { $gt: 25 } })
// Estimated count (faster, less accurate)
db.users.estimatedDocumentCount()
Output:
Total users: 150
Users over 25: 87
🔹 Complete Example
Here's a practical example combining various find operations:
// Switch to database
use storeDB
// Find all products
db.products.find()
// Find expensive products
db.products.find({ price: { $gte: 500 } })
// Find electronics under $1000
db.products.find({
category: "Electronics",
price: { $lt: 1000 }
})
// Get top 5 most expensive products
db.products.find()
.sort({ price: -1 })
.limit(5)
.projection({ name: 1, price: 1 })
// Find products in stock
db.products.find({
stock: { $gt: 0 }
}).count()
// Search by multiple categories
db.products.find({
category: { $in: ["Electronics", "Computers"] },
price: { $lte: 2000 }
}).sort({ price: 1 })
Output:
{ name: "Gaming Mouse", price: 79 }
{ name: "Mechanical Keyboard", price: 149 }
{ name: "Monitor 27\"", price: 399 }
{ name: "Laptop Pro", price: 1299 }