mongodb 4.0事务对node.js的支持 [英] mongodb 4.0 transactions support for node.js

查看:125
本文介绍了mongodb 4.0事务对node.js的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到针对node.js的mongodb 4.0事务支持的文档

cannot find documentation for mongodb 4.0 transactions support for node.js

它已经在mongo驱动程序中可用 http://mongodb.github.io/node-mongodb-native/3.1 /api/

Has it already been available in mongo driver http://mongodb.github.io/node-mongodb-native/3.1/api/

推荐答案

也如注释中所述,您可以在

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.

MongoDB多文档事务的文档还包含示例Node.js代码段.例如:

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:

  • ClientSession.startTransaction()
  • ClientSession.commitTransaction()
  • ClientSession.abortTransaction()

除了MongoDB Node.js驱动程序v3.1,请注意,多文档事务仅可用于MongoDB v4.0.x上的副本集.从版本v4.2开始,可以使用分片群集的事务.

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.

这篇关于mongodb 4.0事务对node.js的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆