'Loopback.JS: How to query with a math expression between properties of a model?

How to query in Loopback with a math expression between properties of a model? Lets say we have a model with some numbers:

{
   "name": "myModel",
   "base": "PersistedModel",
   "idInjection": true,
   "options": {
      "validateUpsert": true
   },
   "properties": {
      "length": {
         "type": "number"
      },
      "width": {
         "type": "number"
      }
   }...
}  

How do I query for models where a formula that involves multiple properties needs to be evaluated? For example return all models where (length + width) > X ? Is there a way to include a model's property in a "where" query:

var MyModel = app.models.MyModel;
MyModel.find( {where: { ??? }}  // how to express something like:  
                                //  (model.length + model.width)<X  


Solution 1:[1]

You can perform that query utilizing collection adapters.

In your case the below code will work:

var myModelCollection = app.models.myModel.dataSource.adapter.collection("myModel");
myModelCollection.find({$where:"this.length + this.width < " + x}).toArray(function(err,returnedResult){
    console.log(err, returnedResult)
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tashakori