mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

undefined is converted to null for Boolean (#9223)

Open josephstgh opened this issue 6 years ago • 4 comments

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

josephstgh avatar Jul 17 '20 11:07 josephstgh

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 avatar Jul 19 '20 18:07 vkarpov15

@vkarpov15 but it's a non backward compatible change in a patch release. Isn't this an issue?

sgpinkus avatar Jul 20 '20 11:07 sgpinkus

@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

creativearmenia avatar Jul 21 '20 15:07 creativearmenia

@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.

vkarpov15 avatar Jul 23 '20 16:07 vkarpov15