mongodb-js / data-service

The MongoDB Data Service
Apache License 2.0
7 stars 4 forks source link

fix: Explicitly pass read preference with db.command commands COMPASS-4539 #282

Closed Anemy closed 3 years ago

Anemy commented 3 years ago

In 3.6.3 the driver updated how operations are run so that operations which have the aspect WRITE_OPERATION default to primary instead of using the connection's readPreference: https://github.com/mongodb/node-mongodb-native/commit/ddcd03d0f934d11a0707dd7d89bc4cb165775f55

This PR updates our use of the db.command function which is one of those operations with the aspect WRITE_OPERATION to explicitly pass the readPreference which the user set up in Compass. This makes it so we have the same behavior as before and previous direct connections to secondaries don't suddenly stop working.

I think we could do a lot of cleanup here. I'm also not quite sure why we're using db.command for listing databases instead of db.listDatabases. This should fix the issue though. We also shouldn't be reaching into db.s.readPreference. I can create a tech debt ticket 🤔

I'm not sure if anywhere else in our codebase we rely on db.command follow the readPreference of the connection, so I'm looking into that.

Connecting and looking at data in a secondary with a direct connection:

Screen Shot 2020-12-15 at 12 33 00 PM