MongoDB logoMongoDB INTERMEDIATE

MongoDB Reference

Complete MongoDB reference with query operators, aggregation, and best practices

5 min read
mongodbdatabasenosqlaggregation

CRUD Operations

Create, Read, Update, and Delete documents in MongoDB

Insert Documents

Add new documents to a collection

javascript
🟢 Essential - Every MongoDB app needs insert operations
💡 MongoDB automatically adds _id if not provided
⚡ insertMany() is faster than multiple insertOne()
📌 Returns insertedId(s) for tracking
⚠️ Check writeConcern for production apps
crudinsert

Find Documents

Query and retrieve documents from collections

javascript
🟢 Essential - Most common MongoDB operation
💡 findOne() returns first match only
📌 Use projection to reduce network transfer
⚡ Create indexes for frequently queried fields
🔗 Related: explain() to analyze query performance
crudreadquery

Update Documents

Modify existing documents in collections

javascript
🟢 Essential - Keep data up-to-date
💡 Use $set to update specific fields only
⚠️ updateMany() can affect many documents - be careful
📌 upsert: true creates doc if not found
⚡ findOneAndUpdate() returns the document
crudupdate

Delete Documents

Remove documents from collections

javascript
⚠️ Delete operations are permanent - no undo
💡 Consider soft delete for important data
📌 deleteMany({}) removes ALL documents
🟢 Essential - Clean up old/unused data
⚡ findOneAndDelete() returns deleted document
cruddelete

Query Operators

Powerful operators for filtering and matching documents

Comparison Operators

Compare field values in queries

javascript
🟢 Essential - Foundation of MongoDB queries
💡 $eq is implicit when using key: value
📌 $in is like SQL IN operator
⚡ Combine operators for complex queries
🔗 Related: $exists, $type for field checks
queryoperatorscomparison

Logical Operators

Combine multiple query conditions

javascript
💡 Implicit AND when listing multiple fields
📌 $or requires array of conditions
⚡ $nor useful for exclusion logic
⚠️ Complex logic can impact performance
🔗 Related: $where for JavaScript expressions
queryoperatorslogical

Array Operators

Query and match array fields

javascript
🟢 Essential for working with arrays
💡 Simple value matches any array element
📌 $elemMatch for complex array element conditions
⚡ Index array queries for better performance
⚠️ $size doesn't accept ranges (use $where)
queryoperatorsarrays

Text Search

Full-text search capabilities

javascript
💡 Text index required for $text search
📌 $text searches all indexed fields
⚡ Text indexes can be large - use wisely
⚠️ Only one text index per collection
🔗 Consider Atlas Search for advanced needs
querysearchtext

Update Operators

Operators for modifying document fields and arrays

Field Update Operators

Modify document field values

javascript
🟢 Essential - Most common update operations
💡 $set creates field if it doesn't exist
📌 Use dot notation for nested fields
⚡ $inc is atomic - safe for concurrent updates
⚠️ $unset removes field entirely
updateoperatorsfields

Array Update Operators

Modify array fields in documents

javascript
💡 $addToSet prevents duplicate values
📌 $ positional operator updates matched element
⚡ $push with $each for bulk array updates
⚠️ $pull removes ALL matching elements
🔗 Related: $pullAll, $pushAll (deprecated)
updateoperatorsarrays

Aggregation Pipeline

Transform and analyze data with pipeline stages

Basic Pipeline Stages

Common stages for data transformation

javascript
🟢 Essential - Powerful data processing
💡 Pipeline stages process sequentially
⚡ $match early to reduce documents
📌 $group _id can be complex expression
🔗 Related: $facet for multiple pipelines
aggregationpipeline

Aggregation Operators

Operators for calculations and transformations

javascript
💡 $sum: 1 counts documents
📌 Use $$ for variables in expressions
⚡ $cond for if-then-else logic
🟢 Essential for data analysis
🔗 Related: $map, $reduce for array processing
aggregationoperators

Indexes & Performance

Optimize query performance with proper indexing

Index Types

Different index types for various use cases

javascript
🟢 Essential - Indexes make queries fast
💡 Compound indexes support multiple query patterns
⚠️ Too many indexes slow down writes
📌 Index order matters in compound indexes
⚡ Use explain() to verify index usage
indexesperformance

Query Optimization

Analyze and optimize query performance

javascript
💡 explain() shows query execution plan
📌 "COLLSCAN" means no index used (slow)
⚡ Profiler helps find slow queries
⚠️ hint() forces index but use carefully
🔗 Related: MongoDB Compass for visual analysis
performanceoptimization

Schema Design & Validation

Design patterns and data validation strategies

Schema Validation

Enforce document structure and data types

javascript
🟢 Essential - Ensure data integrity
💡 JSON Schema provides rich validation
📌 validationLevel controls when to validate
⚠️ Validation adds small performance overhead
🔗 Related: Mongoose for application-level validation
schemavalidation

Design Patterns

Common MongoDB schema design patterns

javascript
💡 Embed for 1:1 or 1:few relationships
📌 Reference for 1:many or many:many
⚡ Denormalization improves read performance
⚠️ Avoid unbounded arrays (use bucketing)
🟢 Essential - Choose pattern based on access patterns
schemapatternsdesign