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

Version 5 changes how undefined value is handled compared to version 4 #8504

Closed
Niffy opened this issue Jan 13, 2020 · 1 comment
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Niffy
Copy link

Niffy commented Jan 13, 2020

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

What is the current behavior?
I have updated from mongoose mongoose@4.13.20 to mongoose@5.8.7, using mongo 3.4.20
I have noticed in version 5 when setting a value as undefined, it is saved as null.
In version 4 this would not have been set but ignored.
This happens when the minimize flag is set to false.

If the current behavior is a bug, please provide the steps to reproduce.

#!/usr/bin/env node
'use strict';

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mongoose5');
const minimize = false
var dummySchema = new mongoose.Schema({
  alpha: { type: String },
  beta: { type: String },
  betaIsNull: { type: Boolean },
  betaIsUndefined: { type: Boolean },
  isMinimize: { type: Boolean }
},
{
  toObject: {virtuals: true, minimize: minimize},
  toJSON: {virtuals: true, minimize: minimize}
}
);
mongoose.model('Dummy', dummySchema);

var dummy1 = new mongoose.models.Dummy({ alpha: 'blabla', beta: null, betaIsNull: true, isMinimize: minimize });
var dummy2 = new mongoose.models.Dummy({ alpha: 'blabla', beta: undefined, betaIsUndefined: true , isMinimize: minimize });

dummy1.save(async (error, saved) => {
  dummy2.save(async (error, saved) => {
    var res = await mongoose.models.Dummy.find({})
    console.log(res)
    return mongoose.connection.close();
  })
});

Output without minimize

[ { _id: 5e1ca7638d47a72f3a086116,
    alpha: 'blabla',
    beta: null,
    betaIsNull: true,
    isMinimize: false,
    __v: 0,
    id: '5e1ca7638d47a72f3a086116' },
  { _id: 5e1ca7638d47a72f3a086117,
    alpha: 'blabla',
    beta: null,
    betaIsUndefined: true,
    isMinimize: false,
    __v: 0,
    id: '5e1ca7638d47a72f3a086117' } ]

Output with minimize

[ { _id: 5e1ca7e42da4fa302e51ee8e,
    alpha: 'blabla',
    beta: null,
    betaIsNull: true,
    isMinimize: true,
    __v: 0,
    id: '5e1ca7e42da4fa302e51ee8e' },
  { _id: 5e1ca7e42da4fa302e51ee8f,
    alpha: 'blabla',
    betaIsUndefined: true,
    isMinimize: true,
    __v: 0,
    id: '5e1ca7e42da4fa302e51ee8f' } ]

What is the expected behavior?
I expected the behavior to be the same in version 4 when using 5, in that undefined values are ignored, with or without the minimize flag.

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

  • mongoose@4.13.20
  • mongoose@5.8.7
  • mongo 3.4.20

comments
In order to stop this, in mongoose 5 I have to set the minimize flag to true. Without this I end up saving documents with a load of null values, which will affect queries when using '$exists' operator.

Why has this changed? I can bypass it by setting minimize to true, but I'm unaware of the consequences.

@vkarpov15 vkarpov15 added this to the 5.8.10 milestone Jan 17, 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 Jan 17, 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 Jan 21, 2020
@Niffy
Copy link
Author

Niffy commented Jan 22, 2020

@vkarpov15 Thank you for fixing this, very much appreciated!

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

2 participants