1
1
const { MongoClient } = require ( 'mongodb' ) ;
2
2
3
- // CRUD operations in transactions must be on existing collections, so be sure you have run
4
- // usersCollection.js prior to running this script.
3
+ // In MongoDB 4.2 and earlier, CRUD operations in transactions must be on existing collections
4
+ // See https://docs.mongodb.com/manual/core/transactions/#transactions-api for more information
5
+ // Be sure you have run usersCollection.js prior to running this script
5
6
6
7
async function main ( ) {
7
8
/**
@@ -64,10 +65,10 @@ async function createReservation(client, userEmail, nameOfListing, reservationDa
64
65
const reservation = createReservationDocument ( nameOfListing , reservationDates , reservationDetails ) ;
65
66
66
67
// Step 1: Start a Client Session
67
- // See http ://bit.ly/Node_startSession for the startSession() docs
68
+ // See https ://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html#startSession for the startSession() docs
68
69
const session = client . startSession ( ) ;
69
70
70
- // Step 2: Optional. Define options to use for the transaction
71
+ // Step 2: Optional. Define options for the transaction
71
72
const transactionOptions = {
72
73
readPreference : 'primary' ,
73
74
readConcern : { level : 'local' } ,
@@ -77,7 +78,7 @@ async function createReservation(client, userEmail, nameOfListing, reservationDa
77
78
try {
78
79
// Step 3: Use withTransaction to start a transaction, execute the callback, and commit (or abort on error)
79
80
// Note: The callback for withTransaction MUST be async and/or return a Promise.
80
- // See http ://bit.ly/Node_withTransaction for the withTransaction() docs
81
+ // See https ://mongodb.github.io/node-mongodb-native/3.6/api/ClientSession.html#withTransaction for the withTransaction() docs
81
82
const transactionResults = await session . withTransaction ( async ( ) => {
82
83
83
84
// Important:: You must pass the session to each of the operations
0 commit comments