MongoEngine specify read preference on query

青春壹個敷衍的年華 提交于 2019-12-20 03:11:00

问题


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?


回答1:


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")

The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.



来源:https://stackoverflow.com/questions/37183256/mongoengine-specify-read-preference-on-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!