1

I'm quite new to Meteor and I'd really appreciate some advice.

I have two collections, the Meteor users collection and a collection called awards. The awards collection contains a field named nominees which contains an array of strings which contain the user _ids from the users collection. An awards document looks like this.

{
    "nominees" : [ 
        "fuEaF9mfMDpYzXFh6", 
        "3NBiDrwM63h4J6QKh"
    ],
    "reason" : "Award reason...",
    "nominator" : "HRD5ozqMNcx6boXQR",
    "_id" : "ozi6wio9EdStvja9J"
}

I need to have a league table on the client for users with the most nominated awards.

I've seen some samples for collecting counts of all documents in a collection. for example Meteor subscribe to a count

But I want something like a dictionary array on the client with one entry for each user, with two values, the users _id and the total count of nominations for the user.

I can run something like this to get the data I need but if a limit is applied on the client collection, this is only accurate on the server, I need a way of getting this data to the client.

I'm assuming I'll need to publish a subscription, subscribe on the client and create a local collection for this on the client but I'm unsure how'd I'd go about this.

Meteor.users.find().fetch().map(function(it) {
  return {
    'fullName': it.profile.fullName,
    'awardsCount': Awards.find({
      nominees: {
        $in: [it._id]
      }
    }).count()
  };
});

Any help would be greatly appreciated.

Community
  • 1
  • 1
Sam M
  • 31
  • 4
  • Does it need to be reacitve? If not, aggregations that can't be done on the client are most easily accomplished by using a method call. – David Weldon Nov 20 '14 at 19:49
  • I'd prefer to make this reactive. So ideally that's what I'm looking for. When I tried to do this through a method I struggled to understand how to get the data from an asynchronous method, I understand I'd have to add a callback but how do I then refer to the data that was returned. – Sam M Nov 20 '14 at 19:53
  • The easiest way is to store the returned data into a session variable (or other reactive data source) and then use that same variable in your helpers. – David Weldon Nov 20 '14 at 19:56

0 Answers0