Update Operators
Field Operators
| Operator | Description | Example |
$set | Set field value | {"$set": {"name": "Bob", "address.city": "NYC"}} |
$unset | Remove field | {"$unset": {"temp_field": ""}} |
$inc | Increment number | {"$inc": {"age": 1, "score": -5}} |
$mul | Multiply number | {"$mul": {"price": 1.1}} |
$min | Set to min of current and given | {"$min": {"low_score": 50}} |
$max | Set to max of current and given | {"$max": {"high_score": 99}} |
$rename | Rename field | {"$rename": {"old_name": "new_name"}} |
$currentDate | Set to current timestamp | {"$currentDate": {"updated_at": true}} |
Array Operators
| Operator | Description | Example |
$push | Append to array | {"$push": {"tags": "new-tag"}} |
$pull | Remove from array | {"$pull": {"tags": "old-tag"}} |
$addToSet | Add if not present | {"$addToSet": {"tags": "unique-tag"}} |
$pop | Remove first (-1) or last (1) | {"$pop": {"queue": -1}} |
Multiple operators can be combined in a single update:
{"$set": {"status": "active"}, "$inc": {"login_count": 1}, "$currentDate": {"last_login": true}}