-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
[6.x regression / behavior change?] findOneAndUpdate no longer sets undefined fields as null #11038
Comments
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String,
age: Number
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
useUnifiedTopology: true,});
await mongoose.connection.dropDatabase();
await Test.create({ name: 'Test'});
let entry = await Test.findOneAndUpdate({
name: 'Test',
}, {age: undefined}, {new: true});
console.log(entry);
console.log(await Test.find());
};
run(); |
Indeed:
@vkarpov15 yes please, migration guide entry very welcome. The existence of a migration guide implies to me (and I don't think that's unreasonable) that it's the thing to read rather than skimming through the detailed changelog, as it may merge/explicit breaking changes to be more easily digestible by users. So, 💯 it should cover this breaking change. Thanks for listening. |
We added it to the migration guide, sorry for the inconvenience. We try our best to make sure the migration guide is exhaustive, but in this case we missed a spot. |
@vkarpov15 For the migration guide and my benefit, please explain how to recreate the previous behavior using the new version of mongoose. Are there global or local options that can be set? We have test code that depends on the So far I have been able to avoid upgrading the world by using global options:
But in this case |
@joebowbeer can you please show me an example of what the test code in question looks like? I'd like to see if I can suggest a workaround without reintroducing |
@vkarpov15 The test code is an e2e test. It sends an event that causes the server-under-test to set an entry with an undefined value and then it verifies that the value is null. The server-under-test code is similar to the code in the repro steps provided in the description and in the comment above. The test code is essentially just verifying that the results are as expected: that the cat has In our case the server-under-test was updated to mongoose6 but the test wasn't, and consequently the test fails. My question is: Is there any way to configure the updated server-under-test to reproduce the old behavior? If not, then anything intentionally or unintentionally dependent on its previous behavior may also need to be updated in sync. Should I create a new issue to enhance the documentation? |
There's no way to configure server-under-test to repro the old behavior currently. If you're asserting that updating a key to What suggestions do you have for improving the documentation around this? |
OP here. I'll chime in that I'm in a similar boat of current behavior "broken" by the change. That being said, it was easy enough to patch our code, from setting nulls through implicit behavior to setting nulls explicitly. I tend to provide/expect a high level of backwards compat and sometimes rant about it, but in this case it's very marginal behavior and AFAIK wasn't even documented initially. Said differently, since there was no contract about thing, and since we depended (knowingly or not) on thing anyway, it's our users task to adjust to new thing, and I don't expect anything more in documentation than "behavior changed, please set your nulls explicitly if you depend on the previous behavior". |
@vkarpov15 thanks for clarifying. If there was a way to achieve strict compatibility, then that would be worth documenting. I think the suggested text is a good addition:
|
@ronjouch I agree wholeheartedly about backwards compat. But this was a case where we think that backwards compat means supporting surprising and confusing behavior - changing We'll add some more info to the docs about this. Not sure exactly where, we'll have to take a closer look. |
Fantastic. Thanks Valeri. |
I confirm I am using the latest version of mongoose as of today: 6.0.14
Do you want to request a feature or report a bug?
Unsure. Maybe this is a 6.x bug/regression, but maybe this is a desired behavior change. Halp, please: is this change desired, and was it documented? I couldn't find a trace of it, neither in the mongoose / Migrating from 5.x to 6.x nor in the underlying node-mongodb-native / Changes in 4.x (and how to migrate!) docs.
Steps to reproduce
What is the current behavior?
Mongoose 6
findOneAndUpdate
does not setundefined
valuesWhat is the expected behavior?
Mongoose 5
findOneAndUpdate
used to set undefined values asnull
:→ Is this change desired, and was it documented? I couldn't find a trace of it, neither in the mongoose / Migrating from 5.x to 6.x nor in the underlying node-mongodb-native / Changes in 4.x (and how to migrate!) docs.
What are the versions of Node.js, Mongoose and MongoDB you are using?
The text was updated successfully, but these errors were encountered: