问题
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!
回答1:
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).
来源:https://stackoverflow.com/questions/22772742/fetch-results-from-model-in-waterline-if-property-is-defined