zookzook / elixir-mongodb-driver

MongoDB driver for Elixir
Apache License 2.0
245 stars 63 forks source link

Specifying `read_preference` at `Mongo.start_link` with `replica_set_with_primary` fails at `Mongo.TopologyDescription.select_replica_set_server` #229

Closed zraul123 closed 9 months ago

zraul123 commented 9 months ago

We've been using the mongodb-driver with the following option at start_link

Mongo.start_link(..., read_preference: Mongo.ReadPreference.slave_ok(%{mode: :primary_preferred})

In 1.2.1 this was changed, and I tried:

  1. Setting it to %{mode: :primary_preferred} directly -> fails at commands such as addIndex since it expects :primaryPreferred
  2. Setting it to Mongo.ReadPreference.to_replica_set(%{mode: :primary_preferred}) which basically translates it to %{mode: :primaryPreferred}. This works for single-instances, sharded instances - but when the topology is replica_set_with_primary it fails at Mongo.TopologyDescription.select_replica_set_server since it can't pattern match. The expected atom is :primary_preferred (line 200 in topology_description.ex), and it's called with :primaryPreferred.

I can't seem to create a test-case to reproduce this exact issue, have you considered adding some integration tests with docker on this repo? I can help if so. Thanks!

zookzook commented 9 months ago

Yes, you are right. This is a bug. I will fix it. Thank you for reporting!

zookzook commented 9 months ago

The read-preferences should only used for read operations. Creating an index is not a read operation, so I will check, why the read-preferences is sent to the MongoDB in this case.

zookzook commented 9 months ago

It is fixed. Feel free to test it and maybe reopen/create a new issue!