Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch results from model in Waterline if property is defined

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!

like image 453
Andrew Carreiro Avatar asked Mar 31 '14 21:03

Andrew Carreiro


1 Answers

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).

like image 174
sgress454 Avatar answered Oct 02 '22 16:10

sgress454