MongoDB - Atomic Operations
MongoDB does now not guide multi-report atomic transactions.it does provide atomic operations on a unmarried record.
Model Data for Atomic OperationsThe advocated method to hold atomicity would be to keep all the related facts, which is often updated together in a unmarried document using embedded files. Consider the subsequent merchandise report
"_id":1, "product_name": "Google Pixel 3 Just Black 128", "category": "mobiles", "product_total": five, "product_available": three, "product_bought_by": [ "customer": "David", "date": "4-Oct-2018" , "customer": "Anderson", "date": "5-Oct-2018" ]Here purchaser who buys the product within the product_bought_by discipline.Whenever a new purchaser buys the product, we will first test if the product is still to be had the usage of product_available discipline. But We can use findAndModify command for this capability because it searches and updates the report inside the identical pass.
>db.products.findAndModify( question:_id:2,product_available:$gt:0, replace: $inc:product_available:-1, $push:product_bought_by:customer:"rob",date:"nine-Jan-2014" )findAndModify question makes sure that the product purchase data is updated only if it the product is available.