Last active
May 28, 2018 20:58
-
-
Save sarjarapu/89cb6c982b6c99cadf3d377f3ebae371 to your computer and use it in GitHub Desktop.
MongoDB commands illustrating multiple transactions with no write conflicts can successfully commitTransactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// v4.0.0-rc0/bin/mongo --port 38000 | |
// **************************************************** | |
// On a sample person collection with two documents _id 1, 2 | |
// Insert new document, _id 3, inside session1 scope | |
// Update a document, _id 1, in session2 scope | |
// Delete a document, _id 2, directly on collection | |
// Understand how the find operation on these scopes change | |
// from beginning of transaction till after commit | |
// **************************************************** | |
// drop and recreate person collection with 2 documents _id 1, 2 | |
use test; | |
db.person.drop(); | |
db.person.insert({"_id": 1, "fname": "fname-1", "lname": "lname-1"}); | |
db.person.insert({"_id": 2, "fname": "fname-2", "lname": "lname-2"}); | |
// create session1 and a collection object using session1 and start a transaction on it | |
var session1 = db.getMongo().startSession(); | |
var session1PersonColl = session1.getDatabase('test').getCollection('person'); | |
session1.startTransaction({readConcern: {level: 'snapshot'}, writeConcern: {w: 'majority'}}); | |
// create session2 and a collection object using session2 and start a transaction on it | |
var session2 = db.getMongo().startSession(); | |
var session2PersonColl = session2.getDatabase('test').getCollection('person'); | |
session2.startTransaction({readConcern: {level: 'snapshot'}, writeConcern: {w: 'majority'}}); | |
// The find operations on all collections inside/outside transactions show same data | |
db.person.find() | |
// { "_id" : 1, "fname" : "fname-1", "lname" : "lname-1" } | |
// { "_id" : 2, "fname" : "fname-2", "lname" : "lname-2" } | |
session1PersonColl.find() | |
// { "_id" : 1, "fname" : "fname-1", "lname" : "lname-1" } | |
// { "_id" : 2, "fname" : "fname-2", "lname" : "lname-2" } | |
session2PersonColl.find() | |
// { "_id" : 1, "fname" : "fname-1", "lname" : "lname-1" } | |
// { "_id" : 2, "fname" : "fname-2", "lname" : "lname-2" } | |
// insert a document _id 3, inside a transaction/session1 | |
session1PersonColl.insert({"_id": 3, "fname": "fname-3", "lname": "lname-3"}); | |
// WriteResult({ "nInserted" : 1 }) | |
// update a document _id 1, inside transaction/session2 | |
session2PersonColl.updateOne({"_id": 1}, {"$set": {"fname": "fname-1U", "lname": "lname-1U"}} ); | |
// { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } | |
// delete a new document directly on the collection | |
db.person.deleteOne({"_id": 2}); | |
// { "acknowledged" : true, "deletedCount" : 1 } | |
// The find operations on all collections inside/outside transactions show different set of data | |
// notice that all the uncommitted data changes inserts/updates are not visible on db.person | |
db.person.find(); | |
// { "_id" : 1, "fname" : "fname-1", "lname" : "lname-1" } | |
// notice that the insert on session1 is only visible to it. | |
// the delete operation is not visible to session1 | |
session1PersonColl.find() | |
// { "_id" : 1, "fname" : "fname-1", "lname" : "lname-1" } | |
// { "_id" : 2, "fname" : "fname-2", "lname" : "lname-2" } | |
// { "_id" : 3, "fname" : "fname-3", "lname" : "lname-3" } | |
// notice that the update on session2 is only visible to it. | |
// the delete operation is not visible to session2 | |
session2PersonColl.find() | |
// { "_id" : 1, "fname" : "fname-1U", "lname" : "lname-1U" } | |
// { "_id" : 2, "fname" : "fname-2", "lname" : "lname-2" } | |
// commit and end the session1 and session2 | |
session1.commitTransaction() | |
session1.endSession() | |
session2.commitTransaction() | |
session2.endSession() | |
// The find operation on the collection now shows committed changes | |
db.person.find() | |
// { "_id" : 1, "fname" : "fname-1U", "lname" : "lname-1U" } | |
// { "_id" : 3, "fname" : "fname-3", "lname" : "lname-3" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment