Sails.js populate with where

user4747674 picture user4747674 · May 14, 2015 · Viewed 7k times · Source

I need to select users with dogs (pets with type equal 'dog')

var User = Waterline.Collection.extend({

  identity: 'user',

  attributes: {
    firstName: 'string',
    lastName: 'string',

    pets: {
      collection: 'pet',
      via: 'owner'
    }
  }
});

var Pet = Waterline.Collection.extend({

  identity: 'pet',

  attributes: {
    type: 'string',
    name: 'string',

    owner: {
      model: 'user'
    }
  }
});

I didn't find any examples, I tried like this

User.find().populate('pets', {type: 'dog'}).exec(err, users) ...

and this

User.find().where({'pets.type': 'dog'}).populate('pets').exec(err, users) ...

but that does not work

Would be greate if result users array will has no pets records

Answer

Agustín picture Agustín · Jul 4, 2016

Did you try this?

User.find().populate('pets', {
  where: {
    type: 'dog'
  }
}).exec(err, users)...