Mongodb v4.0事务,MongoError:事务编号仅在副本集成员或mongos上允许 [英] Mongodb v4.0 Transaction, MongoError: Transaction numbers are only allowed on a replica set member or mongos

查看:2460
本文介绍了Mongodb v4.0事务,MongoError:事务编号仅在副本集成员或mongos上允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了MongoDB v4.0以获得其最强大的功能 Transaction ,并使用mongodb 3.1作为驱动程序.

当我尝试使用交易会话时,遇到了此错误:

MongoError:事务编号仅允许用于副本集成员或mongos.

那是什么,我如何摆脱它?

任何建议都值得赞赏.

解决方案

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

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

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

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

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:

MongoError: Transaction numbers are only allowed on a replica set member or mongos.

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事务,MongoError:事务编号仅在副本集成员或mongos上允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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