Aggregation Pipeline Update Guide
Introduction
This page is the deep guide for aggregation pipeline updates in monSQLize update methods. If you only need to choose between traditional update operators and pipeline payloads, start with Update Methods Overview.
Why do we need an aggregation pipeline?
Limitations for legacy updates:
Advantages of Aggregation Pipelines:
Core Competencies
- ✅ Calculate between fields: Calculate new values based on existing field values
- ✅ Conditional assignment: Set different values based on conditions
- ✅ Multi-stage conversion: Complex data conversion process
- ✅ Fully Compatible: Consistent with MongoDB 4.2+ native syntax
Quick start
Basic example
Identification rules
monSQLize automatically detects update syntax:
Aggregation pipeline basics
Basic structure
An aggregation pipeline is an array of stages:
Available stages
The following stages are available in update:
Field reference
Reference fields using the `Reference fields using the prefix:
Common operators
Arithmetic operators
$add - addition
$subtract - Subtraction
$multiply - Multiplication
$divide - Division
$mod - take the modulus
Conditional operator
$cond - conditional expression
$ifNull - Null value handling
$switch - Multiple branch selection
String operators
$concat - string concatenation
$toLower / $toUpper - Case conversion
$substr - substring
Comparison operators
Logical operators
Usage scenarios
Scenario 1: Calculation between fields
Scenario 2: Conditional assignment
Scenario 3: Data cleaning
Scenario 4: Multi-stage conversion
Scenario 5: Price adjustment
Best Practices
1. Performance first
2. Use intermediate variables
3. Defensive Programming
4. Keep historical data
5. Batch operation
Performance optimization
1. Reduce pipeline stages
2. Use index
3. Process large amounts of data in batches
Notes
1. MongoDB version requirements
Aggregation pipeline updates require MongoDB 4.2+:
2. Field reference syntax
3. Array vs object
4. Performance considerations
Aggregation pipeline updates are slightly slower than traditional updates (~10-20%), only use when necessary:
5. Transaction support
Aggregation pipeline updates fully support transactions:
API Reference
updateOne
Parameters:
filter: query conditionsupdate: Update content (object = traditional update, array = aggregation pipeline)options: Optional configuration
Return:
updateMany
Similar to updateOne, but can update multiple documents.
Related documents
- updateOne guide - updateOne full document
- updateMany guide - updateMany full document
- Update Methods Overview - Method selection and traditional operator overview
- Aggregation pipeline guide - Detailed explanation of aggregation pipeline