Andrew Carreiro
Andrew Carreiro

Reputation: 1567

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!

Upvotes: 5

Views: 4204

Answers (1)

sgress454
sgress454

Reputation: 24948

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

Upvotes: 8

Related Questions