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

minimongo use of undefined doesn't match mongo shell #1646

Closed
datacarl opened this issue Dec 2, 2013 · 3 comments
Closed

minimongo use of undefined doesn't match mongo shell #1646

datacarl opened this issue Dec 2, 2013 · 3 comments

Comments

@datacarl
Copy link
Contributor

datacarl commented Dec 2, 2013

Mini-mongo doesn't treat undefined in a $in lookup the same way as the server does. On the server it matches both null and undefined, but on the client it doesnt match any of them.

  Collection.insert({name: [null]});
  Collection.insert({name: [undefined]});
  Collection.insert({name: ['test']});

//server
console.log(Collection.find({name : {$in : [undefined]}}).count()); // -> 2

//client
console.log(Collection.find({name : {$in : [undefined]}}).count()); // -> 0
@glasser
Copy link
Contributor

glasser commented Dec 3, 2013

The use of undefined in Mongo is pretty inconsistent. The BSON spec (BSON is the object format developed for Mongo's wire protocol) says that undefined is deprecated, and in fact the Node Mongo driver doesn't know how to read or write undefined.

So when you insert things with undefined via Meteor (or anything using the Node Mongo driver), they get converted to null. Similarly, when the Meteor server executes a query with undefined it gets converted to null by the Node Mongo driver. On the other hand, on the client we don't serialize the query to BSON so it tries to actually match undefined against the nulls and gets nothing.

In general we do want minimongo's behavior to match Mongo's. Mongo's behavior with undefined in queries is pretty inconsistent, though! If I use the mongo shell (which, while JavaScript, does not use the Node mongo driver and is capable of differentiating undefined from null) it says:

> db.x.find({name: undefined})
error: { "$err" : "can't have undefined in a query expression", "code" : 13629 }

even though when you put it in a $in it "works".

BTW: if you insert something with undefined in the Mongo shell, the undefined does get into the database, but the Mongo shell still prints it as null. You can see that here:

> db.x.insert({name: [null]})
> db.x.insert({name: [undefined]})
> db.x.insert({name: ['test']})
> db.x.find()
{ "_id" : ObjectId("529d5c8122de5335bffe0b79"), "name" : [  null ] }
{ "_id" : ObjectId("529d5c8422de5335bffe0b7a"), "name" : [  null ] }
{ "_id" : ObjectId("529d5c8722de5335bffe0b7b"), "name" : [  "test" ] }
> db.x.find({name: {$in: [undefined]}})
{ "_id" : ObjectId("529d5c8422de5335bffe0b7a"), "name" : [  null ] }

See how the find matches 1 document (not 2 or 0)? That's because the two nulls in the first find are different!

We should probably try to make Minimongo match Mongo better, but frankly avoiding undefined in your Mongo queries would be advisable.

@datacarl
Copy link
Contributor Author

datacarl commented Dec 3, 2013

Thanks for the detailed response! I'll stay away from undefined!

@glasser glasser closed this as completed Apr 22, 2014
@dotnetwise
Copy link

undefined properties should be ignored at the time of serialization - otherwise you need to delete a property by using the very slow operator delete. Please reopen this issue

hwillson added a commit to hwillson/meteor that referenced this issue Dec 6, 2017
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 pushed a commit that referenced this issue Dec 13, 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 #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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants