Multi-Statement transactions

Mult-statement transactions can be executed using the beginTransaction command

Begin transaction executes multiple DML statements on one or more collections. All Insert, Update and Delete documents specified must execute succesfully or else no modifications are made to the database.

Collections created in the transaction are atomic operations and cannot be rolled back.


This feature is only available from your Nodechef Cloud Search instance. None of the examples provided below will work on a mongodb database. To deploy a cloud search instance, login and navigate to the dashboard. From deployments > deploy cloud search, you can then spin up a cloud search server

Syntax

db.runCommand({ multiStatementTransaction : 1, "<collection_name>.insert" : { <document> }, "<collection_name>.update" : { q: <query>, u: <update_doc>, upsert: <bool>, multi: <bool> }, "<collection_name>.delete" : { q : <query>, limit : <int> }, # Insert multiple documents into a collection "<collection_name>.insertM" : [{ <document> },{...}], # Multiple update statements to be executed on a collection "<collection_name>.updateM" : [{ q: , u: , upsert: , multi: }, {...}], # Multiple delete statements to be executed on a collection "<collection_name>.deleteM" : [{ q : , limit : },{...}], })


Example

db.runCommand({ multiStatementTransaction : 1, "Comments.insert" : { _id : ObjectId("507f191e810c823293de860ea"), text : "Agreed!", date : ISODate("2013-10-02T01:11:18.965Z"), userId : ObjectId("507f191e810c19729de860ea"), PostId : ObjectId("507f1f77bcf86cd799439011"), }, "NewsFeed.update" : { q : { _id : ObjectId("507f1f77bcf86cd799439011") }, u : { $inc : { aggregated_comment_count : 1 } }, } })