Home:ALL Converter>mongodb not using local database in replica set

mongodb not using local database in replica set

Ask Time:2019-01-30T05:07:40         Author:Andrew

Json Formatter

I have 4 servers each hosting a website and a mongo replica set.

  • MongoDB version: v3.4.13
  • Driver: PHP

Server 1 is PRIMARY.

  • Located on the west coast

Server 2 is SECONDARY with tag: { location: 'east' }

  • Located on the east coast

  • I'm connecting to the db with connection string: mongodb://localhost:27017/?replicaSet=rs&readPreference=nearest&readPreferenceTags=location:east

Server 3 and 4 are SECONDARY with no tags.

I want server 2 to read from it's local database, but instead it is reading from primary (or another secondary, I can't tell, but it's definitely not reading from it's local database)

  • I suspect it's not reading from it's own SECONDARY db because there is about a 3 second lag for any query I run.

How can I tell server 2 to read from it's own local SECONDARY db?

Author:Andrew,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54429584/mongodb-not-using-local-database-in-replica-set
simagix :

Include readPreferenceTags in your connection string. Refer to the online doc.\n\nBy the way, you shouldn't have a even number of nodes in a replica set unless one of them has no voting right.",