undefined is converted to null for Boolean (#9223)
Hi,
With reference to https://github.com/Automattic/mongoose/pull/9223, if I define the Boolean field as undefined, it will convert to null. Is that the correct behavior?
Example:
const mongoose = require('/home/sam/src/mongoose/index.js');
const { Schema } = mongoose;
mongoose.Schema.Types.Boolean.convertToFalse.add(null);
console.log(mongoose.Schema.Types.Boolean.convertToFalse);
const M = mongoose.model('Test', new Schema({ a: String, b: Boolean }));
console.log(new M({ a: 'hi', b: undefined })); // hi, null
My previous behavior was that it will only create a field only. b will not be created
Correct behavior. The MongoDB Node driver converts undefined to null: https://github.com/mongodb/js-bson#why-does-undefined-get-converted-to-null because the MongoDB 'undefined' type is deprecated. So Mongoose typically treats nullish values as equivalent.
@vkarpov15 but it's a non backward compatible change in a patch release. Isn't this an issue?
@vkarpov15 setting ignoreUndefined to true should solve the issue and ignore undefined values right? but seems its ignored I am on version 5.9.24
@sgpinkus @creativearmenia can you please open up new issues and follow the issue template. There were no changes related to this particular issue, so I'm not sure what non-backward compatible change you're referring to.