【问题标题】:Get distinct pair of objects with all sub documents mongo meteor使用所有子文档 mongo meteor 获取不同的对象对
【发布时间】:2016-02-04 10:07:04
【问题描述】:

我正在使用流星,并且自 3 天以来一直难以解决此问题。我搜索到 stackoverflow,但答案似乎不能满足我的需求。我有一个销售表,上面有以下数据。

{
"_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")
},   

我期望添加所有数量的不同表名

{
"name":"Hot Coffee",
"quantity": 3
},
{
"name":"Pizza",
"quantity": 7
}

我尝试了 distinct 功能,但它似乎只显示一个结果。

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    为此使用 aggregation framework,但由于 Meteor 尚不支持聚合,因此您需要安装聚合框架包 - 它没有做任何花哨的事情,只是包装为你准备了一些 Mongo 方法。

    只需添加流星 meteorhacks:aggregate,您就可以开始营业了。这将为 Meteor 添加适当的聚合支持。

    现在您需要此管道来实现所需的结果。在 mongo shell 中,运行以下命令:

    var pipeline = [
        {
            "$group": {
                "_id": "$name",
                "quantity": { "$sum": "$quantity" }
            }
        },
        {
            "$project": {
                "name": "$_id", "_id": 0, "quantity": 1
            }
        }
    ];
    
    db.sales.aggregate(pipeline);
    

    样本输出

    {
        "result" : [ 
            {
                "quantity" : 7,
                "name" : "Pizza"
            }, 
            {
                "quantity" : 3,
                "name" : "Hot Coffee"
            }
        ],
        "ok" : 1
    }
    

    此聚合操作的作用是使用 $group 管道步骤按 name 字段对所有文档进行分组,并且对于每个不同的组,您可以使用累加器运算符 $sum 对每组的数值字段数量。下一个管道 $project 会将先前管道流文档中的字段重塑为所需的结构。

    在 Meteor 中,您可以使用以下模式将这些结果发布到客户端的 Sales 集合,前提是您已将聚合包添加到您的流星应用程序:

    Meteor.publish('getDistinctSalesWithTotalQuantity', function (opts) {
    
        var initializing = 1;
    
        function run(action) {
    
            // Define the aggregation pipeline ( aggregate(pipeline) )
            var pipeline = [
                {
                    "$group": {
                        "_id": "$name",
                        "quantity": { "$sum": "$quantity" }
                    }
                },
                {
                    "$project": {
                        "name": "$_id", "_id": 0, "quantity": 1
                    }
                }
            ];
    
            Sales.aggregate(pipeline).forEach(function(e){
                this[action]('distinct-sales', e.name, 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();
        });
    });
    

    以上观察变化,如有必要,重新运行聚合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 2021-06-28
      • 2014-08-14
      • 1970-01-01
      • 2016-03-08
      • 2011-05-08
      • 1970-01-01
      • 2016-01-24
      相关资源
      最近更新 更多