Collection find query conversion when value is undefined


#1

what is actually happening
collectionName.find({id: aVar, isActive: true}) is returning all documents where isActive is true when the variable “aVar” is undefined

Desired Behavior
when aVar is undefined it should return no documents (as none will be active and have an id of undefined)

What I think is happening
I have a vague recollection of reading that meteor applies some transformation to the query object argument to the find function for compatibility reasons. either it was dropping undefined values or converting them to null. However this is not in the documentation of the find function, and I can’t find that information anymore.

My Question

  • Is “what I think is happening” happening?
  • What are the best practices for handling queries with values that may sometimes be undefined?

Thanks in advance!


#2

From the Meteor 1.6.1 release notes:

Meteor’s Node Mongo driver is now configured with the ignoreUndefined connection option set to true, to make sure fields with undefined values are not first converted to null, when inserted/updated. Fields with undefined values are now ignored when inserting/updating. Issue #6051 PR #9444


#3

By default Collection.find queries run as a '$and` so this should not be the case. If you’re code is accurate though, you are matching on the id field rather than the _id field. This should have other effects on the query though, so it seems weird that this would run properly at all.


#4

Thank you that’s exactly where i remember seeing that!