So I've gotten by with Mongo till now, without having to do anything that complex. But now I'm up against something.
I've got an Publisher model and a User model.
- Users have an array of publisherIds.
- so user.following = [1,2,3,4];
I'm building an admin table, and I need to show all publishers, and their number of followers.
Obviously I can't loop over each publisher and run a mongo query there, so what approach should I take?
Collection 1 Users
{
id: 1,
name: 'fred',
following: [1,2,3,4],
},{
id: 2,
name: 'andy',
following: [1,2]
},{
id: 3,
name: 'stephen',
following: [1]
}
Desired output of collection 2 Publishers
{
publisherId: 1,
numberOfFollowers: 3
},{
publisherId: 2,
numberOfFollowers: 2
},{
publisherId: 3,
numberOfFollowers: 1
}