-
-
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
Under certain circumstances undefined values are recorded in database as null values #3169
Comments
Notes:
|
Thanks for the detailed report, will investigate within the next couple days. |
This is a tricky issue - our bson serializer treats undefined as null, so you will never save something with bson type undefined to mongodb. That's one issue. The other subtlety is that saved['more'] = {
beta : 'blabla2',
gamma : undefined,
delta : 'blabla2'
} is not the same as saved['more'] = {
beta : 'blabla2',
delta : 'blabla2'
} These are two different objects - the first object has 3 keys, the second has 2. In this case, I think mongoose is doing the right thing by trying to save |
There is a matter of consistency: The BSON serializer doe not treat gamma = undefined as gamma = null with:
or with
and in the case reported, it only occurs with updates, not with inserts. Generally, mongoose does not record undefined fields in database, and I think it is the right thing to do. Changing undefined values for null values is definitely wrong. |
so any fix on this? |
@bonesoul it was fixed with a driver update almost 3 years ago in c6c0b3c. Are you running into this now? If so, can you please create a new issue with a code sample that demonstrates the problem? If I run the repro script from this issue on mongoose 5.1.7: 3169.js#!/usr/bin/env node
'use strict';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/gh-3169');
var dummySchema = new mongoose.Schema({
alpha: { type: String },
more: {
beta: { type: String },
gamma: { type: Number },
delta: { type: String }
}
});
mongoose.model('Dummy', dummySchema);
var dummy = new mongoose.models.Dummy({ alpha: 'blabla', more: { beta: 'blabla' } });
dummy.save(function (error, saved) {
saved.alpha = 'blabla2';
saved['more'] = {
beta: 'blabla2',
gamma: undefined,
delta: 'blabla2'
};
saved.save(function (error, updated) {
console.log(updated);
return mongoose.connection.close();
});
}); I get: Output:
It works correctly. |
To reproduce (with mongoose 3.8.33 and 4.07 + mongodb 3.0.4 and nodejs 0.12.7 on windows 8.1):
As you can see below gamma is null instead of being discarded
The text was updated successfully, but these errors were encountered: