Created
May 28, 2018 20:25
-
-
Save sarjarapu/7eb7f9df15958ab9971aef9be0b38b25 to your computer and use it in GitHub Desktop.
MongoDB shell commands illustrating the newly added document in the transaction is not visible on the db.person or session1 person collection objects when the session1 is aborted.
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 | |
// Understand how the find operation on these scopes change | |
// from startTransaction to abortTransaction | |
// **************************************************** | |
// 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'}}); | |
// insert a document _id 3, inside a transaction/session1 | |
session1PersonColl.insert({"_id": 3, "fname": "fname-3", "lname": "lname-3"}); | |
// WriteResult({ "nInserted" : 1 }) | |
// find the documents from collection and session1 | |
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" } | |
// { "_id" : 3, "fname" : "fname-3", "lname" : "lname-3" } | |
// commit and end the session | |
session1.abortTransaction() | |
session1.endSession() | |
// show the documents after aborting the transaction | |
db.person.find() | |
// { "_id" : 1, "fname" : "fname-1", "lname" : "lname-1" } | |
// { "_id" : 2, "fname" : "fname-2", "lname" : "lname-2" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment