2

I have this collection (Spieltag) with two documents in MongoDB:

0: Object Note:2.5 SaisonID:201516 SpielerID:105 SpieltagID:1 Tore:1 _id:"vkD5sMCdZdntoCFGP"

1: Object Note:3 SaisonsID:201516 SpielerID:105 SpieltagID:1 Tore:0 _id:"PrqokMS47K4vx4KR4"

I want to summarize Note (2.5+1) with a "where clause" on SpielerID.

This is what I have tried to use:

Spieltag.aggregate({ $match: {
        { SpielerID: { $gte: 105 } }
} },
{ $group: { _id : null, sum : { $sum: "$Note" } } });

But it doesn't work, throwing Aggregate is not a function. Any idea what's wrong?

1

1 Answer 1

3

First, you need to add the aggregate package for Meteor :

meteor add meteorhacks:aggregate

Second, you must pass an array parameter in aggregate like :

Spieltag.aggregate([{
  $match: {
    SpielerID: { $gte: 105 },
  },
}, {
  $group: {
    _id: null,
    sum: { $sum: '$Note' },
  },
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

I made an update... meteorhacks:aggregate added, version 1.3.0 meteorhacks:collection-utils added, version 1.2.0 mongo-livedata added, version 1.0.9 And I insert the above aggregation function to the java script console in chrome. But i get the same error. Must I insert a statement into my app that this will work?
I'm sorry, as mentionned in github.com/meteorhacks/meteor-aggregate, aggregate only works on the server side. To use it on the client you can publish the result of the aggregate function from the server and subscribe to it in the client.
What I have to do when you say that I must build the aggregate function on the server side? In my js File I have the following structure... if (Meteor.isClient) { ... } if(Meteor.isServer) { ... } At the moment in the Server is only Meteor.startup(function () { }); and the anything else is in the isClient "area"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.