Sails.js populate with where

后端 未结 3 946
难免孤独
难免孤独 2020-12-21 05:18

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

var User = Waterline.Collection.extend({

  identity: \'user\',

  attributes: {         


        
3条回答
  •  粉色の甜心
    2020-12-21 06:05

    What you are looking for hasn't been implemented in waterline (Sails ORM) yet, check issue #266 for more details.

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

    This will return all users (User.find()) and only populate pets of type dog (populate('pets', {type: 'dog'})). So you'll have users without dogs in your results.

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

    Waterline does not support dot (.) notation. Sails-mongo does have some support for it due to MongoDB support.

    Finally, if you are using one of the SQL adapters you may work around this by doing a raw sql query using .query() (docs).

提交回复
热议问题