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

inconsistent minimongo insertion #6051

Closed
peonmodel opened this Issue Jan 21, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@peonmodel

peonmodel commented Jan 21, 2016

minimongo has inconsistent behaviour with regards to undefined

depending on where the collection is inserted, {a: undefined} will become {} or {a: undefined} or {a:null}


if(Meteor.isClient){
  Test = new Mongo.Collection(null); // local client collection
  Test.insert({_id: 'client-side-local', a: undefined});
  var R = Test.findOne('client-side-local');
  console.log(R);  //  returns {_id: 'client-side-local', a: undefined}  // key 'a' is unchanged
}
Test2 = new Mongo.Collection('non-local'); // non-local collection
Test2.insert({_id: 'client'+Meteor.isClient, a: undefined});  // insert in both client and server side

var R1 = Test2.findOne('clienttrue');
console.log(R1);  //  returns {_id: 'clienttrue'}  // key 'a' is deleted due to stringify()
var R2 = Test2.findOne('clientfalse');
console.log(R2);  // returns {_id: 'clientfalse', a: null}  // key 'a' value undefined is changed to null

shouldn't the local collection consistently delete keys with undefined as value just like how the object will be stringify when sent over the wire

local collection already replace File objects into empty object, just like what one expect from stringify, but not for undefined
e.g.

//Client-side
TTT = new Mongo.Collection(null);
var obj = {
  file: new File([1, 2, 3, 4, 5], "name of file.xyz", {
    type: 'text/xyz'
  }),
  undef: undefined,
}
TTT.insert(obj);
TTT.findOne()  // returns {file: {}, undef: undefined};
nobj = EJSON.stringify(obj)  // returns "{"file":{}}"
EJSON.parse(nobj)  // returns {file:{}}
@timbotnik

This comment has been minimized.

timbotnik commented Feb 2, 2016

Seems like consistency would be the preferred strategy here.

@peonmodel

This comment has been minimized.

peonmodel commented Feb 3, 2016

i realised that this issue may be related to this: #5301
it appears that the deletion of undefined keys causes the server to interpret the selector as "{}" which it then returns everything

@hwillson

This comment has been minimized.

Member

hwillson commented Aug 16, 2017

This is still happening with the latest version of Meteor (1.5.1 as of today). I've created a reproduction here: https://github.com/hwillson/meteor-issue-6051

undefined is not valid JSON, so it's being stripped via EJSON when passed from the client to the server. It's definitely inconsistent to see undefined converted to null (and saved) when the insert happens on the server. This is happening because Mongo's BSON spec doesn't support undefined, so the Mongo node driver is converting undefined to null, just before saving. We should probably adjust things on the server side to make sure undefined values are stripped instead of being converted to null and inserted.

@hwillson hwillson self-assigned this Dec 5, 2017

hwillson added a commit to hwillson/meteor that referenced this issue Dec 6, 2017

Ignore undefined fields when inserting/updating in Mongo
The Mongo Node driver that Meteor uses currently replaces
`undefined` field values with `null`, when doing an
insert/update. This approach can lead to unexpected behaviour,
as outlined in meteor#1646, meteor#6051 and several other issues. This commit
configures the default Mongo connection to `ignoreUndefined`
fields, which means `undefined` fields are not inserted/updated,
instead of being inserted/updated as `null`.

Fixes meteor#6051.

benjamn added a commit that referenced this issue Dec 13, 2017

Ignore undefined fields when inserting/updating in Mongo (#9444)
* Ignore undefined fields when inserting/updating in Mongo

The Mongo Node driver that Meteor uses currently replaces
`undefined` field values with `null`, when doing an
insert/update. This approach can lead to unexpected behaviour,
as outlined in #1646, #6051 and several other issues. This commit
configures the default Mongo connection to `ignoreUndefined`
fields, which means `undefined` fields are not inserted/updated,
instead of being inserted/updated as `null`.

Fixes #6051.

* Add PR link to History.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment