https://firstname.lastname@example.org?tab=doc#example-Client.UseSessionWithOptions uses the DefaultReadConcern option, which only applies to transactions started within a session, but does not start a transaction. This is confusing, so we should change the example to start a transaction. We should also verify that other sessions-related examples don't do the same thing.
Using a session with readpref=Secondary results in queries against the primary.
Please see the attached test case that demonstrates the issue.
- Local Replica Set called "ci" with two nodes: port 27017 is the primary, 27016 is the secondary
- RS config sets the secondary with priority=0 so it doesn't become primary
- I use toxiproxy to artificially add latency from primary to secondary. This isn't necessary, but makes the test more reliably reproducible.
Debugging FindOne() it seems like the read preference setting for the session is ignored if not inside a transaction. Setting the read preference on the Collection makes the test pass.
I'm roughly following the example from https://godoc.org/go.mongodb.org/mongo-driver/mongo#Client.UseSession . Am I using the API incorrectly?