Giter Site home page Giter Site logo

meteor-aggregate's Introduction

meteorhacks:aggregate

A simple package to add proper aggregation support for Meteor. This package exposes .aggregate method on Mongo.Collection instances.

this only works on server side and there is no oberserving support or reactivity built in

Usage

Add to your app with

meteor add meteorhacks:aggregate

Then simply use .aggregate function like below.

var metrics = new Mongo.Collection('metrics');
var pipeline = [
  {$group: {_id: null, resTime: {$sum: "$resTime"}}}
];
var result = metrics.aggregate(pipeline);

Using Options

var result = new Mongo.Collection('metrics');
var metrics = new Mongo.Collection('metrics');
var pipeline = [
  {$group: {_id: null, resTime: {$sum: "$resTime"}}}
];
var result = metrics.aggregate(pipeline, {explain: true});
console.log("Explain Report:", JSON.stringify(result[0]), null, 2);

Why?

There are few other aggregation packages out there. All of them written with some complex hacks and there are some easy way to do things. They also don't work with custom Mongo drivers as well.

And this package is short and simple. (~20 LOC)

meteor-aggregate's People

Contributors

alethes avatar anthonyastige avatar arunoda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

meteor-aggregate's Issues

Issue

Hi when I use your package I get the following error:

Does $out work?

HI,

I am getting an error Unrecognized pipeline stage name: '$out'

I included { $out: "commentsPerResource" } as my last pipeline stage.

Sorry, I am a bit confusedL: @EventedMind mentioned that it could be a reactive publication... but then both you and he talk about it not being reactive. See http://joshowens.me/using-mongodb-aggregations-to-power-a-meteor-js-publication/

BTW, In my example, I am trying to group the latest 5 comments by a doc_id. I can get this to work with a nested Pipeline call, but it's not reactive, I thought I'd look for the easy way before I started to try and "observeChanges" etc..

ObjectId is incorrectly converted into string by $group

When returning a field containing an ObjectId the returned Object does not contain a valid id-String. When console.logging the direct result on the server the id is not put into ''. So it is actually a hex-number. Obviously the conversion into a string is not done correctly as the resulting string is a direct byte-to-ascii conversion which results in wired characters within the string. It can be corrected by iterating over the result and performing a byte-by-byte 'charCodeAt'-conversion (with zero-padding for numbers < 0x10). Using [email protected].

ObjectID for new MongoInternals.NpmModule.ObjectID(id._str) is not working in 1.4.x

Hi,
i have the issue for using ObjectID in aggregate,
it was working fine before by using ObjectID by new MongoInternals.NpmModule.ObjectID(id._str)
but after upgraded to Meteor 1.4.2.x, it's not working anymore.

The error is as following,
Exception while invoking method 'history' { stack: 'Error: object [""] is not a valid ObjectId\n at serializeObjectId (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/bson/lib/bson/parser/serializer.js:263:11)\n at serializeInto (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/bson/lib/bson/parser/serializer.js:770:17)\n at serializeObject (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/bson/lib/bson/parser/serializer.js:308:18)\n at serializeInto (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/bson/lib/bson/parser/serializer.js:776:17)\n at [object Object].serialize (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/bson/lib/bson/bson.js:58:27)\n at [object Object].Query.toBin (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/mongodb-core/lib/connection/commands.js:140:25)\n at [object Object].Pool.write (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/mongodb-core/lib/connection/pool.js:921:23)\n at [object Object].Cursor._find (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/mongodb-core/lib/cursor.js:287:22)\n at nextFunction (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/mongodb-core/lib/cursor.js:585:10)\n at [object Object].Cursor.next [as _next] (/Users/Yusef/.meteor/packages/npm-mongo/.2.2.16_1.bfrtml++os+web.browser+web.cordova/npm/node_modules/mongodb-core/lib/cursor.js:693:3)',

Allow allowDiskUse:true for mongo 2.6

Hi,

First of all, big thanks for your work for the meteor community, and for this package.

Lately, I'm running into issues that some of my pipelines take too much memory. Since mongo2.6 (I'm running 2.6 in production), the aggregate method of mongo accepts allowDiskUse:true for allowing to sort on disk. Would it be possible to optionally expose this option through the api of your package?

Thanks!
nick.

Not able to install meteor-aggregate in windows

When i run the command "meteor add meteorhacks:aggregate" i get the following errors :

=> Errors prevented startup:

While downloading meteorhacks:[email protected]...:
error: CERT_UNTRUSTED

While downloading meteorhacks:[email protected]...:
error: CERT_UNTRUSTED

=> Your application has errors. Waiting for file change.

If i remove it my app works fine. What do i need to do ? It also gave me one more error saying whether i am offline .

Not handling nulls like in Mongo

Does meteor-aggregate handle a ​_null_​ in an aggregate query different than native Mongo? I have a query that runs fine in Mongo, but returns no records in Meteor using this library. Once I remove the match on a field that is Null in the database, it runs just fine.

Super-simplifed version of the query:

db.expenses.aggregate(
[
{"$match":{
"details.account_number":10940,
"details.vendor_id":null,
"details.dept_id":1}
}
]
)

But once I remove the vendor_id match, then Meteor returns the records.

Is this a bug, or by design?

Thank you.

Publish function returned an array of non-Cursors

Hello,

I' am returning an aggregation on meteor publish and i receiving the following error:

Exception from sub TySCANsTYoXHQCKyq Error: Publish function returned an array of non-Cursors

How do i enable that the aggregate returns an array of cursors?

Matching by _id isn't working

I'm not sure if this is me but for some reason aggregate matching by _id isn't working.

I saw this issue:

#6

and am using the following code but it isn't working? Thoughts?

      let res = collection.aggregate([
        {$match: {'_id': new Mongo.ObjectID('bea1bce7e3276818dd06a34f')}}
      ]);
      console.log(res);

Enhancement Request: Allow Options Parameter After Pipeline

Enhancement request: allow the caller to supply second-positional-parameter options. I saw that there was an earlier request for this feature specifically to enable allowDiskUse, which you mentioned would not work. I have more modest need: to enable explain.

I have hacked together a custom solution on my system:

explainResults = Meteor.wrapAsync(collection.rawCollection().aggregate.bind(collection.rawCollection()))(pipeline, { explain: true });

This seems to work, I'm getting back helpful explain results.

This could be a low-cost/low-risk change that would improve utility of this package. IMHO explain results can be quite helpful when developing complex aggregate queries.

Doesn't work with the following pipeline and dataset

In Meteor:

        var pipeline = [
            {$match: { "shipperZip.start" : {$lte: '07621'},
                       "shipperZip.end"   : {$gte: '07621'}}},
            {$unwind : "$recipientZip"},
            {$match: {"recipientZip.start": {$lte: '90011'},
                      "recipientZip.end":   {$gte: '90011'}}},
            {$project: {"recipientZip.expressZone": 1,
                        "recipientZip.groundZone": 1}}
        ];
        var results = myCollection.aggregate(pipeline);
        console.log(results);

yields
empty square brackets []
no data

In Mongo

db.sampleData.aggregate([{$match: {"shipperZip.start":{$lte: "07621"}, "shipperZip.end": {$gte: "07621"}}}, {$unwind: "$recipientZip"}, {$match: {"recipientZip.start": {$lte: "90011"}, "recipientZip.end": {$gte: "90011"}}}, {$project: {"recipientZip.expressZone": 1, "recipientZip.groundZone": 1}}])

yields:

{
"result" : [
{
"_id" : ObjectId("559ea710df8141168fb60ecd"),
"recipientZip" : {
"expressZone" : "8",
"groundZone" : "8"
}
}
],
"ok" : 1
}

Sample Collection (1 document):

{ 
    "_id" : ObjectId("559ea710df8141168fb60ecd"), 
    "shipperZip" : {
        "start" : "07000", 
        "end" : "07699", 
    }, 
    "recipientZip" : [ 
        {
            "start" : "90000", 
            "end" : "91000", 
            "expressZone" : "8", 
            "groundZone" : "8"
        },
    ]
}

Exception while simulating the effect of invoking 'Collection' TypeError: Collection.aggregate is not a function

Exception while simulating the effect of invoking 'Collection' TypeError: Collection.aggregate is not a function
at Meteor.methods.Totals

I get the above error, when I use aggregate but it does give the results, not sure what is the issue about!!

Collection.aggregate(pipeline);

The following is my packages:
meteor-platform
underscore
materialize:materialize
useraccounts:materialize
less
useraccounts:core
iron:router
accounts-password
sacha:spin
aldeed:collection2
aldeed:autoform
gildaspk:autoform-materialize
pcel:serialize
pcel:loading
matb33:collection-hooks
alethes:pages
meteorhacks:kadira
momentjs:moment
alanning:roles
digilord:faker
pascoual:pdfkit
meteorhacks:aggregate

any suggestions!!

Full $Text $Search with meteor-aggregate throws SyntaxError: Unexpected token $

I've been using meteorhacks:aggregate succesfully for a while now to group article search results by category, (i'm rendering these results into an accordion control that allows each category's results to open and close by clicking the header for that category name) with the various inputs affecting the query output that results in an aggregate query that looks something like this:

[{
    "$match": {
        "$or": [{
            "published": {
                "$exists": false
            }
        }, {
            "published": "false"
        }]
    }
}, {
    "$sort": {
        "category": -1,
        "pubDate": -1
    }
}, {
    "$group": {
        "_id": "$category",
        "category": {
            "$first": "$category"
        },
        "count": {
            "$sum": 1
        },
        "articles": {
            "$push": {
                "category": "$category",
                "_id": "$_id",
                "title": "$title",
                "publisherName": "$publisherName",
                "pubDate": "$pubDate"
            }
        }
    }
}]

this is used in Meteor inside collection.aggregate() and it works beautifully and much faster than my old implementation. So far so good.

Recently I was tasked with improving this to add a text search, so I set up full text indexing and rewrote my code to produce queries like this:

[{
    "$match": {
        "$or": [{
            "published": {
                "$exists": false
            }
        }, {
            "published": "false"
        }],
        $text: {
            $search: "Meteor"
        }
    }
}, {
    "$sort": {
        "category": -1,
        "pubDate": -1
    }
}, {
    "$group": {
        "_id": "$category",
        "category": {
            "$first": "$category"
        },
        "count": {
            "$sum": 1
        },
        "articles": {
            "$push": {
                "category": "$category",
                "_id": "$_id",
                "title": "$title",
                "publisherName": "$publisherName",
                "pubDate": "$pubDate"
            }
        }
    }
}]

While both of these work great on a mongo shell, with db.articles.aggregate(), the one with the $text: { $search: "Meteor" } term fails with an error:
(Note that these are both run against the same database, the issue exists locally in meteor mongo and on compose.io)

I20150501-16:55:10.893(-7)? Exception while invoking method 'getGroupedArticles' SyntaxError: Unexpected token $
I20150501-16:55:10.893(-7)?     at Object.parse (native)
I20150501-16:55:10.893(-7)?     at [object Object].Meteor.methods.getGroupedArticles (packages/mcms-articles/server/articles-server.js:73:1)
I20150501-16:55:10.894(-7)?     at [object Object].methodMap.(anonymous function) (packages/meteorhacks:kadira/lib/hijack/wrap_session.js:160:1)
I20150501-16:55:10.894(-7)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
I20150501-16:55:10.894(-7)?     at packages/ddp/livedata_server.js:648:1
I20150501-16:55:10.894(-7)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150501-16:55:10.894(-7)?     at packages/ddp/livedata_server.js:647:1
I20150501-16:55:10.894(-7)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150501-16:55:10.894(-7)?     at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
I20150501-16:55:10.894(-7)?     at packages/meteorhacks:kadira/lib/hijack/wrap_session.js:50:1

FYI, Without kadira enabled the output looks like this:

I20150501-17:10:19.905(-7)? Exception while invoking method 'getGroupedArticles' SyntaxError: Unexpected token $
I20150501-17:10:19.905(-7)?     at Object.parse (native)
I20150501-17:10:19.906(-7)?     at [object Object].Meteor.methods.getGroupedArticles (packages/mcms-articles/server/articles-server.js:73:1)
I20150501-17:10:19.906(-7)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
I20150501-17:10:19.906(-7)?     at packages/ddp/livedata_server.js:648:1
I20150501-17:10:19.906(-7)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150501-17:10:19.906(-7)?     at packages/ddp/livedata_server.js:647:1
I20150501-17:10:19.906(-7)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150501-17:10:19.906(-7)?     at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
I20150501-17:10:19.906(-7)?     at packages/ddp/livedata_server.js:546:1```

I'm still relatively new at Meteor so I'm not sure how to debug this...

Has anyone been successful using text search within aggregates?

BUG? Mongo aggregate() using $out is not reactive (however collection is reactive)

Mongo's aggregation pipeline is very useful, however using it correctly in Meteor fails silently.

I'm posting here since the community has not succeeded in solving it.
http://stackoverflow.com/questions/31620268/why-is-mongo-aggregation-not-reactive-in-meteor
https://forums.meteor.com/t/why-is-mongo-aggregation-not-reactive-in-meteor/7462

SPEC:
Using Collection.aggregate({ $out: "" }) will output the result of an aggregation pipeline to a mongo collection called . Changes to this should be observable by the meteor server, and UI.

PROBLEM:
Server is not observing the changes to . Using $out DOES successfully create (which can be queried in the Mongo console). Also can be queried from the browser.

The is not invalid. All changes to those documents -- in mongo console for example-- will appear reactively in the UI. The data is not being observed on the server, in repo below see serverMain.js line 8 observeChanges.

REPRODUCING:
See the github with an app that reproduces the error. Just run 'meteor' and go to http://localhost:3000 to see the walk-through to reproduce.
https://github.com/keyscores/meteor-aggregation-issue

DEPENDENCIES:
The app uses meteorhacks:aggregate, a thin wrapper on the Mongo.aggregate()
https://github.com/meteorhacks/meteor-aggregate/

REFERENCE:
Aggregate and $out:
http://docs.mongodb.org/manual/reference/operator/aggregation/out/

Casting issue with Mongo ObjectId()

Not sure if this is a Meteor core issue or to do with how the aggregate pipelines get processed, but trying a $match pipeline with a Mongo ObjectID ends up with the ObjectId object being converted to {_str:"blah"}, and not matching any documents.

[email protected]

After added this package to my project

meteorhacks:aggregate           1.1.0  Proper MongoDB aggregations support for Meteor

Collection.aggregate is undefined

What I need to do?

forcing aggregate pub/sub to rerun

What would best practice be as far as forcing a subscription to flush and then rerun so that I can update an aggregate that is visible on every page?

not able to display the aggregation values in template

I am able to get the aggreate values from server to client, but could not display it on the template. Am i missing something here.Appreciate your help.

//client side javascript
Template.DashboardCategoriesChart.helpers({
'CategoryAggregateItem':function(){
var res;
Meteor.call("getAggregateCategoriesSum",function(errors,results){
console.log("results value: "+ JSON.stringify(results))
return results ;

}
});

//stringfy value returned
results value: [{"_id":"Household","totalAmount":420},{"_id":"Insurance","totalAmount":235},{"_id":"Family","totalAmount":1358},{"_id":"Utilities","totalAmount":5371.5},{"_id":"Automobile","totalAmount":500},{"_id":"Home Office","totalAmount":290},{"_id":"Travel","totalAmount":14},{"_id":"Food","totalAmount":303}]

//Template

            {{#each  CategoryAggregateItem}}
                <tr> 
                    <td>{{_id}}</td><td>{{totalAmount}}</td> 
                </tr> 

            {{/each}}

Pacakges race condition with meteorhacks: aggregate at top and bottom of file

INITIAL ISSUE IS HERE...
However, the issue is now changed to when I put the package at the top of the packages file vs the bottom. See below comment for race condition issue.

When I add meteor-aggregate to my project, running this code:

var metrics = new Mongo.Collection('metrics');
metrics.insert({ sport: 'NFL', contestName: 'eric', entrySize: 35});
metrics.insert({ sport: 'NFL', contestName: 'bobby', entrySize: 30});

var pipeline = [{ $match : { sport : 'NFL'}}, {$project: {contestName: 1, entrySize: 1}}];
metrics.aggregate(pipeline);
var result = metrics.aggregate(pipeline);

I get this error:

W20150111-02:46:41.515(-8)? (STDERR) /Users/SavvyMac/.meteor/packages/meteor-tool/.1.0.38.ydz5xy++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:173
W20150111-02:46:41.516(-8)? (STDERR)                        throw(ex);
W20150111-02:46:41.516(-8)? (STDERR)                              ^
W20150111-02:46:41.520(-8)? (STDERR) TypeError: Object [object Object] has no method 'aggregate'
W20150111-02:46:41.522(-8)? (STDERR)     at app/server/lib/test.js:9:22
W20150111-02:46:41.522(-8)? (STDERR)     at app/server/lib/test.js:12:3
W20150111-02:46:41.523(-8)? (STDERR)     at /Users/SavvyMac/code/meteor-learning/people-match/.meteor/local/build/programs/server/boot.js:175:10
W20150111-02:46:41.523(-8)? (STDERR)     at Array.forEach (native)
W20150111-02:46:41.528(-8)? (STDERR)     at Function._.each._.forEach (/Users/SavvyMac/.meteor/packages/meteor-tool/.1.0.38.ydz5xy++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150111-02:46:41.529(-8)? (STDERR)     at /Users/SavvyMac/code/meteor-learning/people-match/.meteor/local/build/programs/server/boot.js:86:5

There was another issue that mentioned a similar issue, and it was a collection2 error. I don't have collection2 in my project (see below for potential package conflicts).

When I open up meteor mongo, I can run an aggregate query like this, and it returns results, so I know that your package is working:
db.metrics.aggregate([{ $match : { sport : 'NFL'}}, {$project: {contestName: 1, entrySize: 1}}])

These are my packages:

meteor-platform
coffeescript
jquery
accounts-ui
accounts-password
accounts-facebook
insecure
bengott:avatar
iron:router
multiply:iron-router-progress
fourseven:scss
nemo64:bootstrap
less
matteodem:easy-search
meteorhacks:[email protected]
reywood:publish-composite
copleykj:livestamp
copleykj:jquery-autosize
accounts-twitter
zimme:iron-router-active
mquandalle:jade
maazalik:malihu-jquery-custom-scrollbar
momentjs:moment
msavin:sessioninspector
gwendall:session-json
matb33:collection-hooks
vjau:xml2js
http
natestrauser:font-awesome
meteorhacks:npm

mrt:moment-timezone
alanning:roles

npm-container
houston:admin
meteorhacks:kadira
meteorhacks:subs-manager
meteorhacks:aggregate

I tried to debug by

  1. commenting out packages that looked related to collections and their related Collection code
    matb33:collection-hooks and reywood:publish-composite (the error persisted).

  2. started a meteor create project, tested out your aggregate code above (it worked properly). Then added in all the packages above, and ran the same code (it worked properly)

I'm not sure if you have any potential idea why this error is persisting. Thanks!!

Does not work with Meteor.users (my code is on server side)

I have seen issue #4, but I'm afraid that this package still does not work with Meteor.users.
I've spent the whole day trying to get this to work, but it simply won't. (I have the very latest Meteor 1.2 version, for what it's worth).

Background: I had thought it would be a good idea to add customer orders to the user.profile - seemed like an obvious place to put them. So there is an orders[] array under user.profile.
So to see the latest orders I need to unwind this array from all the user records.

The following code works nicely in the mongo console:
> db.users.aggregate([{$match: {}}, {$project: {profile: 1}}, {$unwind: '$profile.orders'}]);

So now here's my (very simple) server code:

Meteor.methods({
    getOrders: function(){
        return Meteor.users.aggregate([{$match: {}}, {$project: {profile: 1}}, {$unwind: '$profile.orders'}]);
    }
});

This fails with the following error message:

I20151021-21:54:50.969(2)? Exception while invoking method 'getOrders' TypeError: Object [object Object] has no method 'aggregate'
I20151021-21:54:50.969(2)?     at [object Object].Meteor.methods.getOrders (meteor://💻app/server/methods.js:5:1)
I20151021-21:54:50.970(2)?     at maybeAuditArgumentChecks (meteor://💻app/livedata_server.js:1692:12)
I20151021-21:54:50.970(2)?     at meteor://💻app/livedata_server.js:708:19
I20151021-21:54:50.970(2)?     at [object Object]._.extend.withValue (meteor://💻app/packages/meteor/dynamics_nodejs.js:56:1)
I20151021-21:54:50.970(2)?     at meteor://💻app/livedata_server.js:706:40
I20151021-21:54:50.970(2)?     at [object Object]._.extend.withValue (meteor://💻app/packages/meteor/dynamics_nodejs.js:56:1)
I20151021-21:54:50.971(2)?     at meteor://💻app/livedata_server.js:704:46
I20151021-21:54:50.971(2)?     at tryCallTwo (/Users/malcolm/.meteor/packages/promise/.0.5.0.15j1xzs++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:45:5)
I20151021-21:54:50.971(2)?     at doResolve (/Users/malcolm/.meteor/packages/promise/.0.5.0.15j1xzs++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:171:13)
I20151021-21:54:50.971(2)?     at new Promise (/Users/malcolm/.meteor/packages/promise/.0.5.0.15j1xzs++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:65:3)

Error: Publish function returned an array of non-Cursors

I'm trying to get joins in meteor mongo based on relation id.
When I used your package we are getting data and printed in console as well.
But I'm implementing it in publish method and getting below error.
Exception from sub usersWithAccount id ESzrf5p2AgHJttckF Error: Publish function returned an array of non-Cursors

So, my question is should I write differently in publish method?

Sharing sample code with you for better understanding.
Meteor.publish('usersWithAccount', function() {
var self = this;
var data = Users.aggregate([{
$lookup: {"localField": "accountId","from": "account", "foreignField": "_id","as":"accountinfo"}
}],
Meteor.bindEnvironment(
function(err, result) {
// Add each of the results to the subscription.
_.each(result, function(e) {
self.added("usersWithAccount", e._id, e);
});
self.ready();
},
function(error) {
Meteor._debug( "Error doing aggregation: " + error);
}
));
return data;
});

Not working with the easiest pipeline

For collection of documents:

{ 
    "_id" : "Et8fYufWoaJwHFY3d", 
    "date" : ISODate("2016-03-03T21:00:00.000+0000"), 
    "group" : {
        "_id" : "fsa5S555zCiJDdxFY", 
        "name" : "Group"
    }, 
    "trainer" : {
        "_id" : "cuZsxTWLWvD7Nqwe7", 
        "name" : "Trainer"
    }, 
    "clients" : [
        {
            "_id" : "JEbz2bXs3d8BDQSSA", 
            "name" : "Client1"
        }, 
        {
            "_id" : "S5N8oJMarDMvievSs", 
            "name" : "Client2", 
            "came" : true
        }
    ]
}

Following

Collection.aggregate(
          {$unwind: '$clients'},
          {$match: {'clients.came': true}},
          {$project: {date: 1, group: '$group.name', trainer: '$trainer.name', client: '$clients.name'}}
        );

Would give us collection of wrong documents

{
          _id: 'Et8fYufWoaJwHFY3d',
          date: ISODate("2016-03-03T21:00:00.000+0000"),
          "group": {
            "_id": "fsa5S555zCiJDdxFY",
            "name": "Group"
          },
          "trainer": {
            "_id": "cuZsxTWLWvD7Nqwe7",
            "name": "Trainer"
          },
          clients: {
            "_id": "S5N8oJMarDMvievSs",
            "name": "Client2",
            came: true
          }
        }

instead of what mongo would give us and what it should be:

{
          _id: 'Et8fYufWoaJwHFY3d',
          date: ISODate("2016-03-03T21:00:00.000+0000"),
          "group": "Group",
          "trainer": "Trainer",
          "client": "Client2"
        }

Issue asigning and converting _id value to string in aggregate concat

I installed the package meteorhacks:aggregate.

I tried an aggregate function to get and concat some values without succes, my issue comes up when I assign an _id to a variable called valueid which is returning a null value.

let ciudadesTotal = ciudades.aggregate(
[
{ $project: { valueid:{$concat: "$ciudades._id".toString()}, label: { $concat: [ "$nombreMunicipio", " - ", "$nombreDepartamento" ] } } }
]
);

The structure of my collection is the following

"_id": {
"_str": "580061f6f35f931501b374e7"
},
"codigoMunicipio": 99773,
"nombreDepartamento": "Vichada",
"nombreMunicipio": "CUMARIBO",

This is what i'm geting as result of my query:

{ _id: 580061f6f35f931501b374e7,
I20161014-02:02:21.088(-5)? value: null,
I20161014-02:02:21.088(-5)? label: 'CUMARIBO - Vichada' } ]

Template data context not being populated

Hi, I'm trying to render the results of an aggregation within a template but it's not working.

The aggregation works based on the console logging, which is an array of the results. The template is being rendered, just not the rows of the table. Have I missed something patently obvious?

Should should the results be a mongodb cursor?

Here's the Meteor method:

colSamples = new Mongo.Collection("sampleCol");

Meteor.methods({
    getSummary: function () {
        var pipeline =
            [
                { $sort: { primaryKey: 1, secondaryKey: 1 } },
                {
                    $group: {
                        _id: { pk: "$primaryKey", sk: "$secondaryKey", pair: { $concat: ["$result1", "$result2"] } },
                        pairFrequency: { $sum: 1 }
                    }
                }
            ];

        console.log(colSamples.aggregate(pipeline));
        return colSamples.aggregate(pipeline);
    }
});

Here's the route:

  this.route('sampleAgg', {
    data: function () { return Meteor.call("getSummary") }
  });

and the template:

<template name="sampleAgg">
    <hr>
    <h2>Sample Aggregate data</h2>

    <table id="s-agg" class="table table-hover table-striped">

            <thead>
                <tr>
                    <th>Prm Key</th>
                    <th>Snd Key</th>
                    <th>Pair</th>
                    <th>Count</th>
                </tr>
            </thead>

            {{#each this}} 
                <tr>
                    <td>{{pk}}</td>
                    <td>{{sk}}</td>
                    <td>{{pair}}</td>
                    <td>{{pairFrequency}}</td>
                </tr>
            {{/each}}

        <tfoot>
        <tr>
          <th>Prm Key</th>
          <th>Snd Key</th>
          <th>Pair</th>
          <th>Count</th>
        </tr>
        </tfoot>

        </table> 

</template>

Versioning

Many thanks for the package! I think the version specification may be off, updating via "meteor update" jumps this to 1.2.0 instead of 1.1.1 as listed in packages.js. Forcing update to 1.1.1 works, just wanted to give a heads up.

Cheers,
-Mike

How to prevent mongoError timeout

I found if running on bulk data Collection, it may cause mongoError timeout.

My mongo Url is mongodb://xxx@x:27017/xxxDb?connectTimeoutMS=0&socketTimeoutMS=0

Would you advise on it?

TypeError: undefined is not a function

Whenever I use the aggregate function as shown below, I always get the following error. Is there a fix for this problem?

Error:

reactiveTable error: argument is not an instance of Meteor.Collection, a cursor, or an array
reporters.js?1419198942494:67 Error: Exception in template helper:
TypeError: undefined is not a function
    at Object.Template.detailedreport.helpers.myCollection (http://localhost:3000/client/views/projects/statsreport.js?08c6386b774103b42916fe6a188e739955d00ca9:81:27)

Code:

Template.statsreport.helpers({
  myCollection: function () {
    var now  = Session.get("startDate");
    var then = Session.get("endDate");
    var custID = Session.get("customer");
    var projID = Session.get("project");

      var pipeline = [
        { $match: { date: { $gte: new Date(now), $lte: new Date(then) },
                    projectid: projID,
                    customerid: custID
            } 
        },
        { $group: {
              _id: { 
                  "projectid": "$projectid",
                  "day": { "$dayOfMonth": "$date" },
                  "month": { "$month": "$date" },
                  "year": { "$year": "$date" }
              },
              totalhours: {"$sum": "$totalhours"}
         }}
      ];
  return Storylog.aggregate(pipeline);
},      
settings: function () {
    return {
        rowsPerPage: 10,
        showFilter: true,
        showColumnToggles: false,
        fields: [
            { key: '_id.day', label: 'Day' },
            { key: 'totalhours', label: 'Hours Spent'}          
        ]
    };
 }
});

Aggregate is untestable

Cannot test it in meteor test. When I'm trying to test SomeCollection.aggregate it throws error: Error: Can only call rawCollection on server collections

BTW is it still maintained by someone?

'aggregate' does not exist in angular2-meteor

Installing package in angular2-meteor project does not work.

To reproduce:

  1. Start with some code from the Socially tutorial
  2. In Terminal Run:
    meteor npm install and meteor
  3. Add package:
    meteor add meteorhacks:aggregate
  4. Add the following code to server\publications\parties:
Meteor.publish('party', function(partyId: string) {
  var metrics = new Mongo.Collection('metrics');
var pipeline = [
  {$group: {_id: null, resTime: {$sum: "$resTime"}}}
];
var result = metrics.aggregate(pipeline);
  return Parties.find(buildQuery.call(this, partyId));
}); 

Error appears in the terminal:
server/imports/publications/parties.ts (13, 22): Property 'aggregate' does not exist on type 'Collection<{}>'.

Usage with Meteor.users

Is it possible to use this package with Meteor.users?
I tried the following code:

  var metrics = Meteor.users;
  var pipeline = [
    {$group: {_id: null, month: {$month: "$createdAt"}}}
  ];
  var result = metrics.aggregate(pipeline);
  return result;

But it throws this error:

I20141106-17:30:35.662(1)? Exception while invoking method 'aggregatedSignups' TypeError: Object #<Object> has no method 'aggregate'
I20141106-17:30:35.662(1)?     at Mongo.Collection.aggregate (packages/meteorhacks:aggregate/index.js:21)
I20141106-17:30:35.662(1)?     at Object.App.Admin.aggregatedSignups (packages/app-admin/server/charts.js:6)
I20141106-17:30:35.662(1)?     at Meteor.methods.aggregatedSignups (packages/app-admin/server/charts.js:13)
I20141106-17:30:35.663(1)?     at methodMap.(anonymous function) (packages/meteorhacks:kadira/lib/hijack/wrap_session.js:179)
I20141106-17:30:35.663(1)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1599)
I20141106-17:30:35.663(1)?     at packages/ddp/livedata_server.js:648
I20141106-17:30:35.663(1)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20141106-17:30:35.663(1)?     at packages/ddp/livedata_server.js:647
I20141106-17:30:35.663(1)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20141106-17:30:35.663(1)?     at _.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646)

Failing on Meteor 1.0.4?

Here's the error I'm getting which I think originates from this package

Exception while invoking method 'usdaLoadFoods' TypeError: Object [object Object] has no method '_getCollection'
I20150320-19:27:33.437(-5)?     at [object Object].MongoInternals.Connection.aggregate (packages/meteorhacks:aggregate/index.js:2:1)
I20150320-19:27:33.437(-5)?     at [object Object].Mongo.Collection.aggregate (packages/meteorhacks:aggregate/index.js:21:1)

There were some Mongo updates mentioned in the Meteor 1.0.4 announcement I wonder if are related.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.