db = db.getSiblingDB('test1'); print (""); db.transaction.drop(); db.test2.drop() function print_test_doc() { print ("") print ("printing committed data in table transaction") result = db.transaction.find().sort({color : 1}) result.forEach( function(p) { print( "person: " + p.person + ", color : " + p.color + ", pencils : " + p.pencils );}); } print (""); print ("Add rows, and count."); session = db.getMongo().startSession() session.startTransaction() t = session.getDatabase("test1").getCollection("transaction") t.insertOne({ person:"mark", color: "red", pencils : 10}); t.insertOne({ person:"john", color: "blue", pencils : 20}); print_test_doc() print ("print table in transaction") result = t.find().sort({color : 1}) result.forEach( function(p) { print( "person: " + p.person + ", color : " + p.color + ", pencils : " + p.pencils );}); print (""); print ("commit the two rows., print again"); session.commitTransaction() print_test_doc() print (""); print ("Add two rows, and count, abort, count."); session = db.getMongo().startSession() t = session.getDatabase("test1").getCollection("transaction") session.startTransaction() t.insertOne({ person:"tristan", color: "brown", pencils : 30}); t.insertOne({ person:"heidi", color: "purple", pencils : 40}); print_test_doc() print ("") print ("print table in session") result = t.find().sort({color : 1}) result.forEach( function(p) { print( "person: " + p.person + ", color : " + p.color + ", pencils : " + p.pencils );}); session.abortTransaction() print ("Abort two rows") print_test_doc()