0

We have data model in loopback where the Customers will have a hasMany relation to Orders, and when i query customers the orders will come in an array for that particular customer and if i want to use existing options given by loopback what is the option i need to use to query to get all the customers with certain order names in AND condition.

Thanks in advance for your help.

Ramya B
  • 65
  • 6

3 Answers3

1

You can query but it would be similar to left join like SQL.

By following query:

Customer.find({
  where:{},
  include: {
     relation: 'orders',
     scope: {
       where: {
           name: 'order name'
       }
     }
  }
}, (err, callback))

results in:

customers array with orders as array
ex: [
     {
        name: customer A,
        orders: [{...},{...}]
     },
     {
        name: customer B,
        orders: [] //That does not match condition in scope
     }
   ]

MoreInfo: Loopback doc reference

But if you want exactly then use native mongo query:

CustomerCollection = Customer.getDataSource().connector.collection("Customer");

and fire aggregate (Native mongo query)

0

If I understand correctly, you mean something like that Customers.find({where: {orderId = 1}}, function (err, cb) ??

if you want to use the And/OR condition, here is an example:

Post.find({where: {and: [{title: 'My Post'}, {content: 'Hello'}]}}, 
          function (err, posts) {
            ...
});

You may find more info in this link: https://docs.strongloop.com/display/public/LB/Where+filter#Wherefilter-and/or

Loay
  • 226
  • 2
  • 8
0

I think you are looking for the filter[include] functionality: http://loopback.io/doc/en/lb2/Include-filter.html

Problem I found here is not supported foreign keys. You can define composite(multiple fields) primary key within the model itself, but you cannot define relations based on composite key{field1, field2, ...], custom workaround .

kensai
  • 943
  • 9
  • 16