0

I want to retreive specific subarray of my document, and I do that:

var userData = Meteor.users.find({_id: this.userId},{groups : {$elemMatch:{id:groupId}}});
    console.log(userData);

But the console.log() return me all data of specific user, no only the subarray, also I tried with findOne.

Someone can help me, please?

julio campos
  • 47
  • 1
  • 6
  • I think what you're trying to achieve is not possible with a simple mongo query. You will need to use aggregations to filter arrays, look here: http://stackoverflow.com/questions/15117030/how-to-filter-array-in-subdocument-with-mongodb – Tomasz Lenarcik Oct 29 '16 at 06:41
  • maybe you could include an example of what is in your database and what you would like to have returned? – Frazer Kirkman Oct 29 '16 at 08:17

1 Answers1

1

You don't need 'groups'. 'Groups' is for group-by request - see https://docs.mongodb.com/v3.2/reference/operator/aggregation/group/

You are just giving search criteria about groupID - so you can do it like:

var userData = Meteor.users.find({_id: this.userId},{groupId: SOMEVARIABLE});
console.log(userData); 
Frazer Kirkman
  • 1,003
  • 1
  • 14
  • 23