Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js populate with where

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

like image 415
user4747674 Avatar asked Jan 09 '23 09:01

user4747674


1 Answers

Did you try this?

User.find().populate('pets', {
  where: {
    type: 'dog'
  }
}).exec(err, users)...
like image 86
Agustín Avatar answered Feb 28 '23 01:02

Agustín