-2

have 2 models with many to many assoc(MongoDB). How I can find in assoc. EXAMPLE USER MODEL

name: {
type:'string'
},
groups:{
collection:'Group'
via:'users'
}

GROUP MODEL

name:{
type:'string'
},
users:{
collection:'User',
via:'groups'
}

how I can find all users in all Groups when name=Jack ?

Eugene
  • 69
  • 1
  • 8

1 Answers1

0

Try:

Group.find().populate('users', {
    name: 'Jack'
}).exec(function(err, groups) {
    // ...
});

UPD: As I understand from your comment you only want groups with Jack-users. So you can just do something like:

User.find({
    name: 'Jack'
}).populate('groups').exec(function(err, users) {
    // ...
});
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79