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

ModifierMeaningExample

$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.

Get MongoDB and Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.