MongoDB Update Modifiers
As mentioned in the section “Updating Documents in a Collection”, MongoDB comes with a set of operators for performing atomic modifications on documents.
Table 2-3. MongoDB update modifiers
| Modifier | Meaning | Example |
|---|---|---|
$inc | Atomic Increment | “$inc”:{"score”:1} |
$set | Set Property Value | “$set”:{"username”:"niall"} |
$unset | Unset (delete) Property | “$unset”:{"username”:1} |
$push | Atomic Array Append (atom) | “$push”:{"emails”:"foo@example.com"} |
$pushAll | Atomic Array Append (list) | “$pushall”:{"emails”:["foo@example.com”,"foo2@example.com"]} |
$addToSet | Atomic Append-If-Not-Present | “$addToSet”:{"emails”:"foo@example.com"} |
$pop | Atomic Array Tail Remove | “$pop”:{"emails”:1} |
$pull | Atomic Conditional Array Item Removal | “$pull”:{"emails”:"foo@example.com"} |
$pullAll | Atomic Array Multi Item Removal | “$pullAll”:{"emails”:["foo@example.com”, “foo2@example.com"]} |
$rename | Atomic Property Rename | “$rename”:{"emails”:"old_emails"} |
As with the MongoDB query operators listed earlier in this chapter, this table is mostly for your reference. These operators will be introduced in greater detail in Chapter 3.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access