Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoEngine specify read preference on query

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?

like image 438
Yahya Avatar asked Feb 14 '26 11:02

Yahya


1 Answers

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.

like image 117
Yahya Avatar answered Feb 17 '26 10:02

Yahya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!