vendredi 20 février 2015

Mongoose query where with missing (or undefined) fields

I have an issue that I have a messy MongoDB database with a revised Mongoose scheme.


I want to be able to query using the 'where' command, but if the field is missing or undefined in any found record then Mongoose returns an error.


For instance:



return this.find({personID: req.user._id})
.where('rangeBefore').lt(time);


This returns an error because some records do not have 'rangeBefore'.


What I want to achieve is that any record without the 'rangeBefore' field is filtered out (i.e. it fails the 'where' test).


I have tried prefixing with 'exists' like this:



return this.find({personID: req.user._id})
.exists('rangeBefore).where('rangeBefore').lt(time);


but I still get an error.


Can anyone suggest a way to ignore records with undefined fields, rather than returning an error?


Aucun commentaire:

Enregistrer un commentaire