pymongo:使用MongoReplicaSetClient的优势? [英] pymongo: Advantage of using MongoReplicaSetClient?

查看:612
本文介绍了pymongo:使用MongoReplicaSetClient的优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 MongoClient MongoReplicaSetClient 可以连接到mongo副本集.实际上,它们的文档页面几乎相同-相同的选项,相同的方法等-区别在于后者的构造函数要求我指定一个copysetSet.

It seems that both MongoClient and MongoReplicaSetClient can connect to mongo replica sets. In fact, their documentation pages are nearly identical - same options, same methods, etc - except that the latter's constructor requires me to specify a replicaSet.

在两种情况下,我们都可以指定读取首选项.在这两种情况下,如果发生降级,我们都必须处理AutoReconnect异常.

In both cases, we may specify a read preference. In both cases, we must handle the AutoReconnect exception if a stepdown occurs.

所以我的问题是:

  1. 为什么一个人要相对另一个使用一个,因为一个人可以对两者执行完全相同的操作?

  1. Why would one use one versus the other, since one can perform the exact same operations with both?

两个都可以执行二级读取,对吗?该文档说ReplicaSetClient的优点是我们可以进行辅助读取,但是显然它们都受支持.

Both can perform secondary reads, correct? The documentation says that the advantage of a ReplicaSetClient is that we can do secondary reads, but clearly they are supported in both.

文档表示ReplicaSetClient具有副本集运行状况监视"功能.这到底是什么意思呢?有没有我可以调用的新方法,这些方法可以告诉我有关Replset的运行状况的信息,而这些功能是我无法使用MongoClient完成的?

The documentation says that the ReplicaSetClient features "replica set health monitoring." What exactly does that mean? Are there new methods I can invoke which tell me about a replset's health that I cannot otherwise do with MongoClient?

理论中,MongoReplicaSetClient将连接到副本集的所有成员,而不仅仅是一个.这是错误的:您可以删减连接字符串中的任何服务器,或者MongoClient和MongoReplicaSetClient仍然可以连接.我想念什么吗?

In theory a MongoReplicaSetClient will connect to all members of the replset, rather than just one. This is false: you may munge or omit any of the servers in the connection string, and both MongoClient and MongoReplicaSetClient are still able to connect. Am I missing something?

推荐答案

这是一个令人困惑的API选择,我们在PyMongo 2.x中感到遗憾.我们将于2015年4月将所有客户端类合并到PyMongo 3中的MongoClient中:

This is was a confusing API choice that we regret in PyMongo 2.x. We will merge all the client classes into MongoClient in PyMongo 3, in April 2015:

http://emptysqua.re/blog/good -idea当时的pymongo-mongoreplicasecasetclient/

同时:

  1. 计划连接到整个副本集时,请使用MongoReplicaSetClient. MongoClient仅连接到一个成员.
  2. 单个MongoReplicaSetClient可用于执行主要或次要读取,以及具有读取首选项的更复杂的决策,请参阅
  1. Use MongoReplicaSetClient when you plan to connect to a whole replica set. MongoClient only connects to one member.
  2. A single MongoReplicaSetClient can be used to perform primary or secondary reads, as well as more sophisticated decision-making with read preferences, see my blog post on the subject. A MongoClient will connect to one member of the replica set (the primary) and always read from it, unless you make a direct connection to a secondary using MongoClient, in which case it will always read from that secondary.
  3. MongoReplicaSetClient monitors the set's health with a background thread that periodically checks on all the members. The client tracks whether members are up, it tracks their ping times, and it notices when a member is added. This will reduce the number of exceptions you see on a flaky network or when the replica set's configuration changes, and it allows the client to correctly implement read preferences.
  4. A MongoReplicaSetClient does in fact connect to all members, whereas a MongoClient only connects to one member. MongoReplicaSetClient tries to connect to each member listed in the connection string; as soon as it connects to one it asks that member for a list of all other members. From this point forward it ignores your connection string and uses the list it got from the member it connected to.

这篇关于pymongo:使用MongoReplicaSetClient的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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