Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 订阅已发布的mongo聚合数据_Mongodb_Meteor - Fatal编程技术网

Mongodb 订阅已发布的mongo聚合数据

Mongodb 订阅已发布的mongo聚合数据,mongodb,meteor,Mongodb,Meteor,我正在使用meteor 1.2.1和db以及以下销售收集数据 { "_id" : 1, "table" : "Table no 2", "name" : "Hot Coffee", "quantity" : 2, "price" : "$10", "seller" : "User", "createdAt" : ISODate("2016-01-06T12:57:17.152Z") }, { "_id" : 2, "table" : "Table no 3A", "name" : "Hot

我正在使用meteor 1.2.1和db以及以下销售收集数据

{
"_id" : 1,
"table" : "Table no 2",
"name" : "Hot Coffee",
"quantity" : 2,
"price" : "$10",
"seller" : "User",
"createdAt" : ISODate("2016-01-06T12:57:17.152Z")
},   
{
"_id" : 2,
"table" : "Table no 3A",
"name" : "Hot Coffee",
"quantity" : 1,
"price" : "$10",
"seller" : "User",
"createdAt" : ISODate("2016-02-01T12:58:17.152Z")
},
{
"_id" : 3,
"table" : "Table no 3A",
"name" : "Pizza",
"quantity" : 2,
"price" : "$50",
"seller" : "User",
"createdAt" : ISODate("2016-01-06T12:58:17.152Z")
},
 {
"_id" : 4,
"table" : "2A",
"name" : "Pizza",
"quantity" : 5,
"price" : "$50",
"seller" : "User",
"createdAt" : ISODate("2016-02-02T11:55:17.152Z")
},   
我在server/publication.js上有以下代码

Meteor.publish('getNewSales', function (opts) {

    var initializing = 1;

    function run(action) {

        // Define the aggregation pipeline ( aggregate(pipeline) )
        var pipeline = [
            {
                "$group": {
                    "_id": "$productName",
                    "quantity": { "$sum": "$quantity" }
                }
            },
            {
                "$project": {
                    "productName": "$_id", "_id": 0, "quantity": 1
                }
            }
        ];

        Sales.aggregate(pipeline).forEach(function(e){
            this[action]('distinct-sales', e.productName, e)
            this.ready()
        });
    };

    // Run the aggregation initially to add some data to your aggregation collection
    run('added');

    // Track any changes on the collection we are going to use for aggregation
    var handle = Sales.find({}).observeChanges({
        added(id) {
            // observeChanges only returns after the initial `added` callbacks
            // have run. Until then, we don't want to send a lot of
            // `self.changed()` messages - hence tracking the
            // `initializing` state.
            if (initializing && initializing--) run('changed');
        },
        removed(id) {
            run('changed');
        },
        changed(id) {
            run('changed');
        },
        error(err) {
            throw new Meteor.Error('Aaaaaaaaah! Grats! You broke it!', err.message)
        }
    });

    // Stop observing the cursor when client unsubs.
    // Stopping a subscription automatically takes
    // care of sending the client any removed messages.
    this.onStop(function () {
        handle.stop();
    });
});
现在我如何订阅聚合数据

到目前为止我所做的:

Tracker.autorun(function(){
  var subscription = Meteor.subscribe('getNewSales');
  if(subscription.ready()){
    console.log(Sales.find({}));
  }
)}

您是否添加了meteorhacks:aggregate?是的,但仍然没有结果