MongoEngine在查询中指定读取首选项 [英] MongoEngine specify read preference on query
问题描述
我正在使用Mongo 2.6,Pymongo 2.7.2和Mongoengine 0.8.7.对于特定的读取查询,我想使用副本集的辅助查询.因此,如mongoengine文档中的此处我的查询如下:
I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows :
from pymongo.read_preferences import ReadPreference
<collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED)
但是,查询总是看起来像是主数据库(此查询的日志始终仅在主数据库中可见).语法正确吗?如果是,如何验证是否正在查询辅助节点?
However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax correct? If yes, how do I verify if the secondary is being queried?
推荐答案
找出问题所在.在MongoEngine 连接" 方法中,需要按以下方式指定 replicaSet 参数:
Figured out what the issue was. In the MongoEngine "connect" method, the replicaSet parameter needed to be specified as follows:
connect(db = "my_db", replicaSet = "my_replica_set_name", host = "hostname", port = "port_number")
读取首选项的语法如上所述是正确的.传入 replicaSet 参数使其生效.
The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.
这篇关于MongoEngine在查询中指定读取首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!