MongoDB connection URI with different readPreferences with readPreferenceTags

I have a connection URI

mongodb://host.example.com/?readPreference=secondaryPreferred&readPreferenceTags=region:uswest;readPreferenceTags=

Now, if I use .WithReadPreference(ReadPreference.Primary) , I understand that it overrides the read preference mentioned in the URI, but does it use the same tag preferences or it just tries to use default server selection algorithm?

If it is default behaviour, how can we make sure we can set readPreferenceTags for different read preferences.

Thanks in advance.

Hello @Vicky ,

Welcome back to the MongoDB forum, hope you are doing well! :wave:

As per this documentation on Tag Set List and Read Preference Modes

Tags are not compatible with mode primary , and in general, only apply when selecting a secondary member of a set for a read operation. However, the nearest read mode, when combined with a tag set list, selects the matching member with the lowest network latency. This member may be a primary or secondary.

Also, if you specify tag set lists or a maxStalenessSeconds value with primary , the driver will produce below error.

MongoInvalidArgumentError : Primary read preference cannot be combined with tags

Please check this doc for reference.

Regards,
Tarun

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.