loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Unable to Query data models based on relational model condition

Open princecharmx opened this issue 10 years ago • 5 comments

Hi,

Scenario: modelA belongs to space modelA has spaceId space has field categoryTag

I want to query modelA where related model data has following data space.categoryTag= "valuex" but I am unable to find a right way.


space : {id:"1", "categoryTag":"valuex"}, {id:"2", "categoryTag":"valuey"}, {id:"3", "categoryTag":"valuex"} {id:"4", "categoryTag":"valuez"}

modelA: {"id":"1", "spaceId":"1"}, {"id":"2", "spaceId":"2"}, {"id":"3", "spaceId":"3"}, {"id":"4", "spaceId":"4"}

Result after querying modelA shoud return: {"id":"1", "spaceId":"1"}, {"id":"3", "spaceId":"3"}

Help needed

princecharmx avatar Jul 08 '16 10:07 princecharmx

@princecharmx : You can use include filter to query related models. Read more here and here. Hope that would resolve your question.

gunjpan avatar Jul 08 '16 18:07 gunjpan

@princecharmx : Closing this for now, should you need further assistance with it, mention me in the comments and I'll be glad to help. Thank you :)

gunjpan avatar Jul 11 '16 19:07 gunjpan

@gunjpan , that will return all modelA data with only related data restricted to matching filter. query modelA :

"where: {
   "include": {
     "relation": "space",
     "scope": { 
       "where": {"categoryTag": "valueX"}
     }
   }
 }
}

output:

{"id":"1", "space":[{id:"1", "categoryTag":"valuex"}]},
{"id":"2", "space":[]},
{"id":"3", "space":[{id:"3", "categoryTag":"valuex"}]},
{"id":"4", "space":[]}

princecharmx avatar Aug 02 '16 16:08 princecharmx

Have you looked at: http://www.the-cat-in-the-hack.com/posts/Loopback-Find-n-Sift/

It isn't an ideal solution (especially if you need to use a limit filter as well) but it will work to give you the results you want.

On Tue, Aug 2, 2016 at 12:17 PM, Pratik Chopra [email protected] wrote:

@gunjpan https://github.com/gunjpan , that will return all modelA data with only related data restricted to matching filter. query modelA :

"where: { "include": { "relation": "space", "scope": { "where": {"categoryTag": "valueX"} } } } }

output:

{"id":"1", "space":[{id:"1", "categoryTag":"valuex"}]}, {"id":"2", "space":[]}, {"id":"3", "space":[{id:"3", "categoryTag":"valuex"}]}, {"id":"4", "space":[]}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/strongloop/loopback/issues/2495#issuecomment-236957313, or mute the thread https://github.com/notifications/unsubscribe-auth/AB-8Wcw_LW0WuQSYbgO1M8MuVN9xHF_aks5qb22wgaJpZM4JH7Jb .

ghost avatar Aug 02 '16 17:08 ghost

Hello, have you solved this problem? That link is effective

ghost avatar Sep 02 '17 09:09 ghost