MongoEngine – Atomic Updates

MongoEngine – Atomic Updates

Atomicity is one of the ACID transaction properties. A database transaction has to be indivisible and irreducible so that it either occurs completely or doesn’t occur at all. This property is called Atomicity. MongoDB supports Atomicity only on single documents and not on multi-document transactions.
MongoEngine provides the following methods for atomic updates on a queryset.
update_one() − Overwrites or adds first document matched by query.
update() − Performs atomic update on fields matched by query.
modify() − Update a document and return it.
Following modifiers may be used with these methods. (These modifiers come before the field, not after).

set set a particular value
unset delete a particular value
inc increment a value by a given amount
dec decrement a value by a given amount
push append a value to a list
push_all append several values to a list
pop remove the first or last element of a list depending on the value
pull remove a value from a list
pull_all remove several values from a list
add_to_set add value to a list only if its not in the list already

The following is an example of atomic update, we first create a Document class called Books and add a document in it.

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField, ListField
connect('mydata', host='mongodb://localhost/mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
topics = ListField(StringField())
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
b1 = Books(book_id="1013", book_name="maths", book_price=672, topics=[
'Algebra', 'Calculus', 'Statistics', 'Arithematic'])
b1.save()

this will gernerate following output:

{
"_id":{"$oid":"6034a64d3350e4814ce49d60"},
"book_id":1013,
"book_name":"maths",
"book_price":672,
"topics":["Algebra","Calculus","Statistics","Arithematic"]
}

Let us use update_one() method to update book_name field from maths to Mathematics.

Books.objects(book_name="maths").update_one(set__book_name='Mathematics')

this will gernerate following output:

{
"_id":{"$oid":"6034a64d3350e4814ce49d60"},
"book_id":1013,
"book_name":"Mathematics",
"book_price":672,
"topics":["Algebra","Calculus","Statistics","Arithematic"]
}

The push modifier is used to add data in ListField (topics).

Books.objects(book_name="Mathematics").update_one(push__topics='Number theory')

this will gernerate following output:

{
"_id":{"$oid":"6034a64d3350e4814ce49d60"},
"book_id":1013,
"book_name":"Mathematics",
"book_price":672,
"topics":["Algebra","Calculus","Statistics","Arithematic","Number theory"]
}

To increment book_price field by 100, we can use inc modifier.

tests.objects(name='MongoDB').update_one(inc__attempts=1)

The updated document looks as follows −

{
"_id":{"$oid":"6034a64d3350e4814ce49d60"},
"book_id":1013,
"book_name":"Mathematics",
"book_price":772,
"topics":["Algebra","Calculus","Statistics","Arithematic","Number theory"]
}
MongoEngine – Document Inheritance (Prev Lesson)
(Next Lesson) MongoEngine – Javascript
', { 'anonymize_ip': true });