I've got a model where not every property is required. I'd like to query the model and return all instances where the property is defined.
Here's what I think the code should look like, but it doesn't work. Any ideas or links to some detailed documentation?
MyModel.find()
.where({
"propertyThatMayExist" : {
"!=" : undefined
}
});
Thanks a bunch in advance!
The easiest way would be to test against null
. The correct operator is !
or not
:
MyModel.find().where({propertyThatMayExist: {'!': null}}).exec(console.log);
This assumes you don't want to sometimes explicitly set the property to null
for an instance, which would be problematic for some databases anyway (think of MySQL, which defaults most fields to NULL if they aren't filled out).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With