Sails.js populate with where

后端 未结 3 945
难免孤独
难免孤独 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).

    0 讨论(0)
  • 2020-12-21 06:10

    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)

    0 讨论(0)
  • 2020-12-21 06:21

    Did you try this?

    User.find().populate('pets', {
      where: {
        type: 'dog'
      }
    }).exec(err, users)...
    
    0 讨论(0)
提交回复
热议问题