Navigation

Upcoming Features in 4.0

Note

The development, release, and timing of any features or functionality described for our products remains at our sole discretion. This information is merely intended to outline our general product direction and it should not be relied on in making a purchasing decision nor is this a commitment, promise or legal obligation to deliver any material, code, or functionality.

➤ Multi-document Transactions

MongoDB 4.0 will add multi-document transactions for replica sets.

Example

Consider a scenario where for each order shipment, you need to decrease the inventory for the item shipped. If either write fails, you want the undo both operations.

The following Python code shows a sample usage of transactions where either both write operations succeed or both operations fail.

client = pymongo.MongoClient(...)
db = client.test

s = client.start_session()
s.start_transaction()

try:
    db.inventory.update_one({"sku": "abc123"}, {"$inc": {"qty": -100}}, session=s)
    db.shipment.insert_one({"sku": "abc123", "qty": 100}, session=s)
except Exception:
    s.abort_transaction()
else:
    s.commit_transaction()
s.end_session()

Beta Program

Sign up for the multi-document transactions beta program.