Skip to content
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

Can't set Boolean to undefined #9275

Closed
aerkmukdahan opened this issue Jul 23, 2020 · 4 comments
Closed

Can't set Boolean to undefined #9275

aerkmukdahan opened this issue Jul 23, 2020 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@aerkmukdahan
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I have doc.booleanField = true and I want to remove this key from my document by this below code

doc.booleanField = undefined
doc.save()

But the result is doc.booleanField = null and the key isn't removed

What is the expected behavior?

It should able to set boolean field to undefined and can use doc.save() to remove it form doc

I was able to remove boolean field by this way until mongoose update to version 5.9.24

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node: 10.20.1
Mongoose: 5.9.25
MongoDB: 4.0.18

@kevinsandow
Copy link

This was introduced by allowing null as a valid property for booleans and it is breaking our code as well. ☹️

@josephstgh
Copy link

Related to this #9253

@sgpinkus
Copy link
Contributor

This was introduced by allowing null as a valid property for booleans and it is breaking our code as well. frowning_face

@KevinBusse null has always been a valid value for booleans. This is a regression somehow introduced in 5.9.24.

@sgpinkus
Copy link
Contributor

Here is a test script for this issue:

const mongoose = require('mongoose');
const { Schema } = mongoose;

async function test() {
  var Test  = mongoose.model('Test', new Schema({ a: Boolean }));

  // TEST 1: INITIALIZATION:
  console.log(new Test({ a: undefined })); // 5.9.23: {}, 5.9.24: { a: null }

  // TEST 2: UPDATE AFTER SAVE:
  await mongoose.connect('mongodb://localhost/test', {});
  var x = new Test({ a: true });
  await x.save();
  x = await Test.findById(x);
  console.log(x);
  x.a = undefined;
  await x.save();
  x = await Test.findById(x);
  console.log(x);  // 5.9.23: {}, 5.9.24: { a: null }
}

test().then(() => process.exit()).catch((e) => { console.error(e); process.exit(); })

@vkarpov15 vkarpov15 added this to the 5.9.26 milestone Jul 27, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jul 27, 2020
vkarpov15 added a commit that referenced this issue Jul 27, 2020
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

5 participants