问题
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