Sails.js populate with where

爷,独闯天下 提交于 2019-11-29 15:21:40

Did you try this?

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

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).

If you don't need to query users and just need the query for dogs. You could just as easily reverse the query.

Pet.find({type: 'dog'}).populate('users').exec(err, petsWithUsers)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!