Closed
Description
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
Activity
glasser commentedon 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 thatundefined
is deprecated, and in fact the Node Mongo driver doesn't know how to read or writeundefined
.So when you insert things with
undefined
via Meteor (or anything using the Node Mongo driver), they get converted tonull
. Similarly, when the Meteor server executes a query withundefined
it gets converted tonull
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 matchundefined
against thenull
s 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 differentiatingundefined
fromnull
) it says:even though when you put it in a
$in
it "works".BTW: if you
insert
something withundefined
in the Mongo shell, theundefined
does get into the database, but the Mongo shell still prints it asnull
. You can see that here:See how the
find
matches 1 document (not 2 or 0)? That's because the twonull
s 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 commentedon Dec 3, 2013
Thanks for the detailed response! I'll stay away from undefined!
dotnetwise commentedon Oct 28, 2015
undefined
properties should be ignored at the time of serialization - otherwise you need to delete a property by using thevery slow
operatordelete
. Please reopen this issueundefined
value insertsnull
instead ofundefined
Automattic/monk#146Ignore undefined fields when inserting/updating in Mongo
Ignore undefined fields when inserting/updating in Mongo (#9444)