Aggregate function not working in publication

I have tried aggregate (sakulstra:aggregate) method inside my publish method for making my result set in alphabet order using collation, but got an error on subscription.

Error: Publish function can only return a Cursor or an array of Cursors

As the error says, a publication can only return Cursor’s - the aggregate function (including when patched by sakulstra:aggregate) returns an array of values.

It is possible to return arbitrary data using the publication system, but it’s not obvious to do, and you are responsible for re-running whatever query (in your case the aggregation) that provides the data: https://docs.meteor.com/api/pubsub.html

Essentially, aggregations are non-reactive. There have been a couple of attempts at making reactive aggregations - and depending on what the aggregation is, they can work quite well, for example: https://github.com/robfallows/tunguska-reactive-aggregate

2 Likes

Thank you @znewsham :slight_smile:

I have tried this package , and getting an error only, i think this package is not stable

error details:-
tunguska:reactive-aggregate [Error]

  • did you set “sub” to “this”

Let’s see the code you used, please.

Meteor.publish(‘dTypes’, () => {

return ReactiveAggregate(this, DTypes, [{$match:{name:“Bag”}}], {});

});

//subscribe

Meteor.subscribe(‘dTypes’);

Without any other information, there are clearly two differences between what you’ve got and what’s required:

  1. You cannot use “fat arrow” syntax (=>) in a publication. You must use function.
  2. You do not return ReactiveAggregate.

Take a look at the basic example in the README.

ok thank you @robfallows wil check this :slight_smile:

Meteor.publish('dTypes', function() {

  ReactiveAggregate(this, DTypes, [{$match:{name:"Bag"}}], {});

});
2 Likes

thank you @robfallows code changed and now got an another error
error: ReactiveAggregate support for Mongo.ObjectID is disabled due to 1 package error

in my collections i am using type string for _id not ObjectId

At the time the publication is instantiated, the package doesn’t know how you’ve organised your _ids. So all that message is saying is that “if your collection(s) use ObjectIDs, you have missing dependencies which you need to fix before you can get ObjectID support.”

It’s a diagnostic message and shouldn’t affect you if you only have string _ids.

You should have got more information than you have posted above, which explains the missing package(s) you would need to install.

here is the complete error log

I20200721-17:13:55.983(5.5)? ReactiveAggregate support for Mongo.ObjectID is disabled due to 1 package error(s):
I20200721-17:13:55.984(5.5)? 1 - simpl-schema: MODULE_NOT_FOUND
I20200721-17:13:56.410(5.5)? Exception from sub dTypes id CCCGEtkwJvuJYL3i7 Error: A pipeline stage specification object must contain exactly one field.
I20200721-17:13:56.411(5.5)? at update (packages/tunguska:reactive-aggregate/aggregate.js:252:13)
I20200721-17:13:56.411(5.5)? at ReactiveAggregate (packages/tunguska:reactive-aggregate/aggregate.js:319:3)
I20200721-17:13:56.411(5.5)? at Subscription. (imports/api/deliveryTypes.js:30:9)
I20200721-17:13:56.412(5.5)? at packages/matb33_collection-hooks.js:307:21
I20200721-17:13:56.412(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.412(5.5)? at Subscription._handler (packages/matb33_collection-hooks.js:306:28)
I20200721-17:13:56.413(5.5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1771:12)
I20200721-17:13:56.413(5.5)? at packages/ddp-server/livedata_server.js:1046:15
I20200721-17:13:56.413(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.414(5.5)? at Subscription._runHandler (packages/ddp-server/livedata_server.js:1044:51)
I20200721-17:13:56.415(5.5)? at Subscription.subscriptionProto._runHandler (packages/mdg:meteor-apm-agent/lib/hijack/wrap_subscription.js:12:24)
I20200721-17:13:56.429(5.5)? at Session._startSubscription (packages/ddp-server/livedata_server.js:860:9)
I20200721-17:13:56.429(5.5)? at Session.sub (packages/ddp-server/livedata_server.js:625:12)
I20200721-17:13:56.429(5.5)? at packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:77:34
I20200721-17:13:56.429(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.429(5.5)? at Session.sessionProto.protocol_handlers.sub (packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:76:44)
I20200721-17:13:56.430(5.5)? Sanitized and reported to the client as: errorClass [Error]: tunguska:reactive-aggregate [Error]
I20200721-17:13:56.431(5.5)? at errorClass. (packages/tunguska:reactive-aggregate/aggregate.js:32:27)
I20200721-17:13:56.431(5.5)? at new errorClass (packages/meteor.js:655:17)
I20200721-17:13:56.431(5.5)? at update (packages/tunguska:reactive-aggregate/aggregate.js:252:13)
I20200721-17:13:56.432(5.5)? at ReactiveAggregate (packages/tunguska:reactive-aggregate/aggregate.js:319:3)
I20200721-17:13:56.432(5.5)? at Subscription. (imports/api/deliveryTypes.js:30:9)
I20200721-17:13:56.432(5.5)? at packages/matb33_collection-hooks.js:307:21
I20200721-17:13:56.439(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.440(5.5)? at Subscription._handler (packages/matb33_collection-hooks.js:306:28)
I20200721-17:13:56.440(5.5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1771:12)
I20200721-17:13:56.440(5.5)? at packages/ddp-server/livedata_server.js:1046:15
I20200721-17:13:56.441(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.441(5.5)? at Subscription._runHandler (packages/ddp-server/livedata_server.js:1044:51)
I20200721-17:13:56.441(5.5)? at Subscription.subscriptionProto._runHandler (packages/mdg:meteor-apm-agent/lib/hijack/wrap_subscription.js:12:24)
I20200721-17:13:56.441(5.5)? at Session._startSubscription (packages/ddp-server/livedata_server.js:860:9)
I20200721-17:13:56.442(5.5)? at Session.sub (packages/ddp-server/livedata_server.js:625:12)
I20200721-17:13:56.442(5.5)? at packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:77:34 {
I20200721-17:13:56.442(5.5)? isClientSafe: true,
I20200721-17:13:56.442(5.5)? error: ‘Error’,
I20200721-17:13:56.443(5.5)? reason: ‘tunguska:reactive-aggregate’,
I20200721-17:13:56.443(5.5)? details: undefined,
I20200721-17:13:56.443(5.5)? message: ‘tunguska:reactive-aggregate [Error]’,
I20200721-17:13:56.443(5.5)? errorType: ‘Meteor.Error’
I20200721-17:13:56.443(5.5)? }
I20200721-17:13:56.444(5.5)?
I20200721-17:13:56.816(5.5)? Exception from sub dTypes id sFwuYhBeh3Kd6nwTe Error: A pipeline stage specification object must contain exactly one field.
I20200721-17:13:56.818(5.5)? at update (packages/tunguska:reactive-aggregate/aggregate.js:252:13)
I20200721-17:13:56.822(5.5)? at ReactiveAggregate (packages/tunguska:reactive-aggregate/aggregate.js:319:3)
I20200721-17:13:56.823(5.5)? at Subscription. (imports/api/deliveryTypes.js:30:9)
I20200721-17:13:56.823(5.5)? at packages/matb33_collection-hooks.js:307:21
I20200721-17:13:56.823(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.823(5.5)? at Subscription._handler (packages/matb33_collection-hooks.js:306:28)
I20200721-17:13:56.823(5.5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1771:12)
I20200721-17:13:56.824(5.5)? at packages/ddp-server/livedata_server.js:1046:15
I20200721-17:13:56.824(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.824(5.5)? at Subscription._runHandler (packages/ddp-server/livedata_server.js:1044:51)
I20200721-17:13:56.824(5.5)? at Subscription.subscriptionProto._runHandler (packages/mdg:meteor-apm-agent/lib/hijack/wrap_subscription.js:12:24)
I20200721-17:13:56.825(5.5)? at Session._startSubscription (packages/ddp-server/livedata_server.js:860:9)
I20200721-17:13:56.825(5.5)? at Session.sub (packages/ddp-server/livedata_server.js:625:12)
I20200721-17:13:56.825(5.5)? at packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:77:34
I20200721-17:13:56.825(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.825(5.5)? at Session.sessionProto.protocol_handlers.sub (packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:76:44)
I20200721-17:13:56.826(5.5)? Sanitized and reported to the client as: errorClass [Error]: tunguska:reactive-aggregate [Error]
I20200721-17:13:56.827(5.5)? at errorClass. (packages/tunguska:reactive-aggregate/aggregate.js:32:27)
I20200721-17:13:56.827(5.5)? at new errorClass (packages/meteor.js:655:17)
I20200721-17:13:56.827(5.5)? at update (packages/tunguska:reactive-aggregate/aggregate.js:252:13)
I20200721-17:13:56.827(5.5)? at ReactiveAggregate (packages/tunguska:reactive-aggregate/aggregate.js:319:3)
I20200721-17:13:56.828(5.5)? at Subscription. (imports/api/deliveryTypes.js:30:9)
I20200721-17:13:56.828(5.5)? at packages/matb33_collection-hooks.js:307:21
I20200721-17:13:56.828(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.828(5.5)? at Subscription._handler (packages/matb33_collection-hooks.js:306:28)
I20200721-17:13:56.829(5.5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1771:12)
I20200721-17:13:56.829(5.5)? at packages/ddp-server/livedata_server.js:1046:15
I20200721-17:13:56.829(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20200721-17:13:56.829(5.5)? at Subscription._runHandler (packages/ddp-server/livedata_server.js:1044:51)
I20200721-17:13:56.830(5.5)? at Subscription.subscriptionProto._runHandler (packages/mdg:meteor-apm-agent/lib/hijack/wrap_subscription.js:12:24)
I20200721-17:13:56.830(5.5)? at Session._startSubscription (packages/ddp-server/livedata_server.js:860:9)
I20200721-17:13:56.830(5.5)? at Session.sub (packages/ddp-server/livedata_server.js:625:12)
I20200721-17:13:56.830(5.5)? at packages/mdg:meteor-apm-agent/lib/hijack/wrap_session.js:77:34 {
I20200721-17:13:56.830(5.5)? isClientSafe: true,
I20200721-17:13:56.831(5.5)? error: ‘Error’,
I20200721-17:13:56.831(5.5)? reason: ‘tunguska:reactive-aggregate’,
I20200721-17:13:56.831(5.5)? details: undefined,
I20200721-17:13:56.831(5.5)? message: ‘tunguska:reactive-aggregate [Error]’,
I20200721-17:13:56.832(5.5)? errorType: ‘Meteor.Error’
I20200721-17:13:56.837(5.5)? }

1 Like
I20200721-17:13:55.983(5.5)? ReactiveAggregate support for Mongo.ObjectID is disabled due to 1 package error(s):
I20200721-17:13:55.984(5.5)? 1 - simpl-schema: MODULE_NOT_FOUND

That’s the warning - it’s not an error.

I20200721-17:13:56.410(5.5)? Exception from sub dTypes id CCCGEtkwJvuJYL3i7 Error: A pipeline stage specification object must contain exactly one field.

That’s the error from the MongoDB library and it’s basically saying your pipeline is incorrect. To my eye your pipeline looks ok, except for the “smart quotes”: “Bag” vs "Bag". Is that your actual pipeline?

1 Like

Thank you very much @robfallows issue fixed, i have installed package simpl-schema and everything is working fine now.

Thank you so much! It’s very helpful to me.

1 Like

@robfallows i faced an issue when deploy the changes to galaxy,the issue is app is being very slow and some functions like sync is not working , so i reverted my changes( unistall tunguska:reactive-aggregate and simpl-schema 1.9.0 and my publications changed to previous state) and the issue is solved. do you have any idea why this happening?
i posted this issue as another post, please check. Thank you.

Most issues to do with poor aggregation performance are down to missing or bad indexes, especially with large collections. I would suggest using the debug parameter to look at what the aggregation is doing. Something like:

ReactiveAggregate(this, collection, pipeline, {
  debug(explain) {
    console.log(JSON.stringify(explain, null, 2));
  }
});

Refer to the official MongoDB driver doc for more information.

1 Like

Thank you will check this.

Hi @robfallows , how can I remove this warning (without installing simpl-schema package since I’m not using it):

I20200721-17:13:55.983(5.5)? ReactiveAggregate support for Mongo.ObjectID is disabled due to 1 package error(s):
I20200721-17:13:55.984(5.5)? 1 - simpl-schema: MODULE_NOT_FOUND
1 Like

In short, you can’t suppress warnings. However, it sounds like a good idea. Please raise a GitHub issue so it can be tracked. Thanks :slightly_smiling_face:

3 Likes