Skip to content

Replace deprecated MongoDB option #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 10, 2021
Merged

Replace deprecated MongoDB option #116

merged 8 commits into from
Jul 10, 2021

Conversation

backflip
Copy link
Contributor

The returnOriginal option for findOneAndUpdate is deprecated, see https://github.com/mongodb/node-mongodb-native/blob/f916843ad2002a4c829c5f126e58e8efba257525/lib/collection.js#L1720

This should get rid of warnings like [MONGODB DRIVER] DeprecationWarning: collection.findOneAndUpdate option [returnOriginal] is deprecated and will be removed in a later version.

@animir
Copy link
Owner

animir commented Jun 17, 2021

@backflip Hi, thank you for this PR.

I think, we can't replace it like that. This package is used with many different versions of node-mongodb-native. The new option was implemented just month ago mongodb/node-mongodb-native@f916843.

One option is to check mongodb-native package version and use appropriate option.

@backflip
Copy link
Contributor Author

Very good point, sorry for missing that. I have added a proposal for reading the mongodb driver version. It looks like the option will be removed with v4: https://github.com/mongodb/node-mongodb-native/releases/tag/v4.0.0-beta.4

@animir
Copy link
Owner

animir commented Jun 23, 2021

@backflip This looks good. Could you also write a couple of tests to make sure it works as expected?

@ainkinen
Copy link

Should this maybe cache the version info into a variable to avoid doing the full getDriverVersion operation on every _upsert?

@animir
Copy link
Owner

animir commented Jul 1, 2021

@ainkinen Very good point. And it would be better if we can get MongoDB driver version from client not from filesystem.
Actually, current solution is not viable at all.

@backflip Could you try to get version from client and store it in the local class variable during class instance construction?

@backflip
Copy link
Contributor Author

backflip commented Jul 1, 2021

@animir, I did not find a way of getting the version from the driver. There was a discussion about exposing it: mongodb/node-mongodb-native#2709

@backflip
Copy link
Contributor Author

backflip commented Jul 1, 2021

Update: It seems to work via client.topology.s.options.metadata.driver.version after successful connection. Let me check this out.

@backflip
Copy link
Contributor Author

backflip commented Jul 1, 2021

@backflip This looks good. Could you also write a couple of tests to make sure it works as expected?

@animir, would you want to replace the stubbed tests or extend them?

@animir
Copy link
Owner

animir commented Jul 1, 2021

@backflip You can stub a connected Mongo Client and test it for a case when client.topology.s.options.metadata.driver.version exists and not.

@@ -235,7 +257,7 @@ class RateLimiterMongo extends RateLimiterStoreAbstract {
const where = Object.assign({ key: rlKey }, docAttrs);

return this._collection.deleteOne(where)
.then(res => res.result.n > 0);
.then(res => res.deletedCount > 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered this while testing with the real clients: v4 does not return result anymore. Seems to be in line with https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered this while testing with the real clients: v4 does not return result anymore. Seems to be in line with https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/

Should it be selected depending on driver version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked fine with 3.6.9, but let me do some digging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was updated in https://github.com/mongodb/node-mongodb-native/pull/2651/files#diff-06ad5e5a8b0f21c69f986da5545842d224dd5a79c054081bc35d714e4bdc6077. However, it just mentions that Our CRUD result types have been out-of-sync with the official drivers CRUD specification for some time. I could not find any old specs where result was part of the response.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@backflip deletedCount is supported at least from MongoDB Server 3.2 http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~deleteWriteOpResult It is safe to use it

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@backflip Did it brake tests, where delete function used? I see 3 mocks with result.n mocked for those tests.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@backflip never mind above question. I see you already took care of that 👍

@backflip
Copy link
Contributor Author

backflip commented Jul 1, 2021

Should this maybe cache the version info into a variable to avoid doing the full getDriverVersion operation on every _upsert?

Updated accordingly. Thanks for the input, @ainkinen!

@backflip
Copy link
Contributor Author

backflip commented Jul 1, 2021

@backflip You can stub a connected Mongo Client and test it for a case when client.topology.s.options.metadata.driver.version exists and not.

Should I remove the non-stubbed tests or keep them? They helped me discover that the delete method was failing in v4 (see comment above).

@ainkinen
Copy link

ainkinen commented Jul 1, 2021

Should this maybe cache the version info into a variable to avoid doing the full getDriverVersion operation on every _upsert?

Updated accordingly. Thanks for the input, @ainkinen!

The path operations and reading the module were the ones that caught my eye earlier. I think the current solution is much better in general. 👍

@animir
Copy link
Owner

animir commented Jul 1, 2021

@backflip There is no infrastructure for real MongoDB connection in test environment. I understand, tests with real database can help in some cases, but it also brings new level of complexity. Let's keep stabbed tests for this PR.

@backflip
Copy link
Contributor Author

backflip commented Jul 1, 2021

@backflip There is no infrastructure for real MongoDB connection in test environment. I understand, tests with real database can help in some cases, but it also brings new level of complexity. Let's keep stabbed tests for this PR.

Fair enough. 👍


it('consume 1 point (driver v3)', (done) => {
const testKey = 'consume1v3';
sinon.stub(mongoClient, 'topology').value({ s: { options: { metadata: { driver: { version: '3.6' } } } } });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@animir, does this make sense as a mock? I'm checking the resulting upsertOptions below.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@backflip yes, this looks good

@animir animir merged commit dd1ebed into animir:master Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants