本文介绍了Mongodb v4.0 Transaction, MongoError: Transaction numbers are only allowed on a replica set member or mongos的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 MongoDB v4.0 以在 Nodejs 中使用 mongodb 3.1 作为驱动程序,以实现其最惊人的功能事务.

当我尝试使用事务会话时,我遇到了这个错误:

MongoError:事务号只允许在副本集成员或 mongos 上.

那是什么,我怎样才能摆脱它?

任何建议表示赞赏.

解决方案

Transactions 无疑是 MongoDB 4.0 中最令人兴奋的新特性.但不幸的是,大多数用于安装和运行 MongoDB 的工具都会启动一个独立的服务器,而不是副本集.如果您尝试在独立服务器上启动会话,则会出现此错误.

为了使用事务,您需要一个MongoDB副本集,并且在本地启动一个副本集进行开发是一个复杂的过程.新的 run-rs npm 模块 使启动副本集变得容易.只需运行 run-rs 即可启动副本集,run-rs 甚至会为您安装正确版本的 MongoDB.

Run-rs 除了 Node.js 和 npm 没有外部依赖.您不需要安装 Docker、自制软件、APT、Python 甚至 MongoDB.

使用 npm 的 -g 标志全局安装 run-rs.您还可以在 package.json 文件的 devDependencies 中列出 run-rs.

npm install run-rs -g

接下来,运行带有 --version 标志的 run-rs.Run-rs 将为您下载 MongoDB v4.0.0.别担心,它不会覆盖您现有的 MongoDB 安装.

run-rs -v 4.0.0 --shell

然后在您的连接字符串中使用 replicaSet=rs.

您可以在此处找到有关它的更多详细信息.>

I've installed MongoDB v4.0 for the most amazing feature of it Transaction in Nodejs with mongodb 3.1 as a driver.

When I try to use a transaction session I've faced this error:

What's that and how can I get rid of it?

Any suggestion is appreciated.

解决方案

Transactions are undoubtedly the most exciting new feature in MongoDB 4.0. But unfortunately, most tools for installing and running MongoDB start a standalone server as opposed to a replica set. If you try to start a session on a standalone server, you'll get this error.

In order to use transactions, you need a MongoDB replica set, and starting a replica set locally for development is an involved process. The new run-rs npm module makes starting replica sets easy. Running run-rs is all you need to start a replica set, run-rs will even install the correct version of MongoDB for you.

Run-rs has no outside dependencies except Node.js and npm. You do not need to have Docker, homebrew, APT, Python, or even MongoDB installed.

Install run-rs globally with npm's -g flag. You can also list run-rs in your package.json file's devDependencies.

npm install run-rs -g

Next, run run-rs with the --version flag. Run-rs will download MongoDB v4.0.0 for you. Don't worry, it won't overwrite your existing MongoDB install.

run-rs -v 4.0.0 --shell

Then use replicaSet=rs in your connection string.

You find more details about it here.

这篇关于Mongodb v4.0 Transaction, MongoError: Transaction numbers are only allowed on a replica set member or mongos的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:41