'How to filter related models (hasMany relation) using where in LoopBack 3

Customer hasMany relationship with jobs

Filtering by the top-level property works as shown below

Customer.find({ include: ["jobs"],where :{username:"mel"} }, function(err, data) {
console.log(data);
cb(null, true, data);
});

But I am unable to filter by a second-level property which has nested data.

I tried the below it but doesn't seem to be working

Customer.find({ include: ["jobs"],where :{"jobs.JobTypes":"Plumbing"} }, function(err, data) {
console.log(data);
cb(null, true, data);
});

I have tried this as well.

Customer.find({ include: ["jobs"],where :{"jobs":{"JobTypes":"Plumbing"}} }, function(err, data) {
console.log(data);
cb(null, true, data);
});

This is the json I am trying to Filter

 {[{"customer_id": 1,
 "customer_name": "1", 
"realm": "string", 
"username": "mel", 
"email": "[email protected]", 
"emailVerified": false, 
"id": "5d88ac24a823fa5504b2db1f", 
"jobs": [ { "jobId": 1, "CustomerId": 1, "jobType": "Painting", "id": "5d88c4670f527a484c57b09d" }, 
{"jobId": 2, "CustomerId": 1, "jobType": "Plumbing", "id": "5d88c47f0f527a484c57b09e" },
 {"jobId": 3, "CustomerId": 1, "jobType": "Dancing", "id": "5d88c4920f527a484c57b09f" } ]


Solution 1:[1]

Customer.find({
  include: {
    relation: 'jobs',
    scope: {
      where: {
        jobType: 'Plumbing'
      }
    }
  }
}, (err, data) => {
  // TODO handle error
  console.log(data)
})

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 Antonio Trapani