Update Operators

Field Operators

OperatorDescriptionExample
$setSet field value{"$set": {"name": "Bob", "address.city": "NYC"}}
$unsetRemove field{"$unset": {"temp_field": ""}}
$incIncrement number{"$inc": {"age": 1, "score": -5}}
$mulMultiply number{"$mul": {"price": 1.1}}
$minSet to min of current and given{"$min": {"low_score": 50}}
$maxSet to max of current and given{"$max": {"high_score": 99}}
$renameRename field{"$rename": {"old_name": "new_name"}}
$currentDateSet to current timestamp{"$currentDate": {"updated_at": true}}

Array Operators

OperatorDescriptionExample
$pushAppend to array{"$push": {"tags": "new-tag"}}
$pullRemove from array{"$pull": {"tags": "old-tag"}}
$addToSetAdd if not present{"$addToSet": {"tags": "unique-tag"}}
$popRemove 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}}