Loopback: Allow filter fields on nested properties

Created on 9 Sep 2015  路  7Comments  路  Source: strongloop/loopback

https://docs.strongloop.com/display/public/LB/Fields+filter

If I have example a location property composed by

  • city
  • state
  • address
  • zip

and I want show only city and state in the results

Person.find({
  fields: {
    id: true,
    location: {
      city: true,
      state: true
    }
  }
}, function(err, people){ ... })

this will returns the whole location property

feature stale

Most helpful comment

@rkmax I know it's been a while but did you find a way to soluce this ?

All 7 comments

@raymondfeng Is this feature supported yet?

+1

This would be very useful

filter: {
    fields: {
        id: true,
        'user.id': false
     }
}

+1

After some investigation, this commit is in part responsible.

With the 2 blocks:

+  if (Array.isArray(options.fields)) {
 +    keys = keys.filter(function(k) {
 +      return (options.fields.indexOf(k) != -1);
 +    });
 +  }

It filters the fields (looking for the field aaa.bbb.ccc in the document and got only the first-depth field aaa) so it won't play well with nested fields.

A right solution would be to check with lodash if the path aaa.bbb.ccc exists in the document,

options.fields = options.fields.forEach(k => {
  return _.has(data, k)
})

And use _.get to filter the returned data.

I think this is possible with the following syntax;

Post.find({
  include: {
    relation: 'owner', // include the owner object
    scope: { // further filter the owner object
      fields: ['username', 'email'], // only show two fields
      include: { // include orders for the owner
        relation: 'orders', 
        scope: {
          where: {orderId: 5} // only select order with id 5
        }
      }
    }
  }
}, function() { /* ... */ });

http://loopback.io/doc/en/lb2/Include-filter.html

@rkmax I know it's been a while but did you find a way to soluce this ?

Was this page helpful?
0 / 5 - 0 ratings