Use an aggregation from both client and server

I have a method that makes several find in DB and i want to use an aggregation instead. How can i use this method from both client and server?

You can’t run aggregations on the client side db (mini mongo) per se, but you can have a method that runs a server-side aggregation and returns its result like so:

meteor.methods({
  'something': param => Collection.rawCollection().aggregate(
    pipeline,
    options
  ).toArray()
});

For reactive aggregation results have a look at GitHub - robfallows/tunguska-reactive-aggregate: Reworks jcbernack:reactive-aggregate for ES6/ES7 and Promises

1 Like

Why would this run properly?

run() {
  if (Meteor.isServer) {
    return runAggregation();
  }
  return null;
}

When running this method, in client side i get null, and then it doesn’t get updated from the server call.

The aggregation might be async unless you used Promise.await()

No, i am not using any async/await, the aggregation is actually a big aggregation pipeline, that goes like:

let result;
...
result = collection.aggregate(pipeline);
// Do some calculation on result
return result;

The aggregation runs correct in server and when i console.log inside the aggregation, i get the result just fine. The problem is that client is not updated correctly