Reputation: 1795
cannot find documentation for mongodb 4.0 transactions support for node.js
Has it already been available in mongo driver http://mongodb.github.io/node-mongodb-native/3.1/api/
Upvotes: 2
Views: 1442
Reputation: 18835
As mentioned on the comment as well, you can find the reference for transactions on node-mongodb-native v3.1 API ClientSession. This is because transactions are associated with a session. That is, you start a transaction for a session. At any given time, you can have at most one open transaction for a session.
The documentation for MongoDB multi-document Transactions also contains examples Node.js
code snippets. For example:
session.startTransaction({
readConcern: { level: 'snapshot' },
writeConcern: { w: 'majority' }
});
const employeesCollection = client.db('hr').collection('employees');
const eventsCollection = client.db('reporting').collection('events');
await employeesCollection.updateOne(
{ employee: 3 },
{ $set: { status: 'Inactive' } },
{ session }
);
await eventsCollection.insertOne(
{
employee: 3,
status: { new: 'Inactive', old: 'Active' }
},
{ session }
);
try {
await commitWithRetry(session);
} catch (error) {
await session.abortTransaction();
throw error;
}
The reference for the methods above can be found on:
In addition to MongoDB Node.js driver v3.1, please note that multi-document transactions are available for replica sets only on MongoDB v4.0.x. Transactions for sharded clusters are available starting with version v4.2.
Upvotes: 5