简体   繁体   English

PyMongo - 使用次要作为种子时写入主要

[英]PyMongo - Write to Primary when using Secondary as seed

I'm trying to connect to my MongoDB and updating a document.我正在尝试连接到我的 MongoDB 并更新文档。 We're using a replica server as a seed and then we want to write a collection (specifically, update a document).我们使用副本服务器作为种子,然后我们想要编写一个集合(特别是更新文档)。

No matter what I do, every time I try to update the given document, I get the following error: NotMasterError: not master, full error: {'ok': 0.0, 'errmsg': 'not master', 'code': 10107, 'codeName': 'NotMaster'} .无论我做什么,每次尝试更新给定文档时,都会收到以下错误: NotMasterError: not master, full error: {'ok': 0.0, 'errmsg': 'not master', 'code': 10107, 'codeName': 'NotMaster'}

I've tried changing the read pereference to Primary, changing the write concern to w: 1 but nothing seems to work.我尝试将读取优先级更改为 Primary,将写入关注更改为 w: 1 但似乎没有任何效果。

When I debug, I can see that the client discovered all the machines in the network, including the actual master.当我调试时,我可以看到客户端发现了网络中的所有机器,包括实际的master。

With a Mongo library in another language (Reactivemongo in Scala), this is done automatically but seems that with PyMongo I'm struggling.使用另一种语言的 Mongo 库(Scala 中的 Reactivemongo),这是自动完成的,但似乎使用 PyMongo 我很挣扎。 How can I ensure that the update gets forwarded to a Primary node?如何确保将更新转发到主节点?

If anybody can help, that'd be great :)如果有人可以提供帮助,那就太好了:)

Read preference applies to reads.阅读偏好适用于阅读。 It has no effect on writes.它对写入没有影响。 All writes must be sent to the primary.所有写入都必须发送到主节点。

You should be connecting to replica set (also known as "discovering the topology") instead of using a direct connection, and then specifying read preference for secondary reads.您应该连接到副本集(也称为“发现拓扑”)而不是使用直接连接,然后为二级读取指定读取首选项。

So thanks to @ D. SM answer, I ensured that when I init the MongoClient, I connect to the specific replicaset by adding the keyword param: client = MongoClient(uri, replicaset='my-replica-set-name') .所以感谢@ D. SM 的回答,我确保在初始化 MongoClient 时,我通过添加关键字参数连接到特定的副本集: client = MongoClient(uri, replicaset='my-replica-set-name')

To find out what the replica set name is (if you don't know it), you can look at your server status and go down to the conf key repl.setName .要找出副本集名称是什么(如果您不知道),您可以查看您的服务器状态并转到 conf 键repl.setName

Thanks again :)再次感谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM