Getting your data structured the way you want seems like a question of whether to use one query with joins vs multiple queries that get you the desired result. This may help regarding the first scenario: $lookup (aggregation) ā MongoDB Manual
Regarding multiple queries, another approach would be to get your groups
data like so: perform first query to get
var groups = {
group1: ['userId1', 'userId2'], //array of user ID's
group2: ['userId3', 'userId4'],
group3: ['userId5', 'userId6'],
}
Then
Object.keys(groups).forEach(function(groupItemArray){
groupItemArray.forEach(theUserId, itemIndex){
groupItemArray[itemIndex] = Meteor.users.findOne(theUserId);
});
});
To be sure, this isnāt valid JS (since arrays donāt have string-keyed properties like, for example, PHP does):
[
group1: [{_id: userId1, profile: {}}, {_id: userId2, profile: {}}],
group2: [{_id: userId3, profile: {}}, {_id: userId4, profile: {}}],
group3: [{_id: userId5, profile: {}}, {_id: userId6, profile: {}}],
]
I think you meant for an array objects like:
[
{group1: [{_id: userId1, profile: {}}, {_id: userId2, profile: {}}]},
{group2: [{_id: userId3, profile: {}}, {_id: userId4, profile: {}}]},
{group3: [{_id: userId5, profile: {}}, {_id: userId6, profile: {}}]},
]