I'm trying to connect to a MongoDB replicaSet using MongoEngine? I would like to connect to any available secondary server.
I can only find pyMongo examples. Any help?
I'm trying to connect to a MongoDB replicaSet using MongoEngine? I would like to connect to any available secondary server.
I can only find pyMongo examples. Any help?
Copyright © 2021 Jogjafile Inc.
If you want to connect to a secondary server you need to specify a read preference such as
SECONDARYorSECONDARY_PREFERRED. Note that when reading data from a secondary you should anticipate the data is eventually consistent and may be stale (i.e. changes may not have replicated from the primary yet).You will want to import
ReadPreferencefrom the basepymongodriver for a list of constants. You can specify a defaultread_preferenceat the connection level, or per query.Example using secondary preferred (will read from primary if secondary is not available):
You can check if reads are going to secondaries using
mongostat --discover.