在一个结果中mongodb多个组

Ala*_*eng 2 mongodb mongodb-query aggregation-framework

我有类似于存储在mongodb中的文档

{
 "_id":"transaction_id"
 "customer":"some customer",
 "order_date":Date('2011-01-01'),
 "delivery_date":Date('2011-01-15'),
 "amt":500.0,
 "qty":50
},
{
 "_id":"transaction_id"
 "customer":"some customer",
 "order_date":Date('2011-01-01'),
 "delivery_date":Date('2011-02-04'),
 "amt":500.0,
 "qty":50
}
Run Code Online (Sandbox Code Playgroud)

我希望对订单日期和交货日期进行一些汇总,以绘制每月订购和交付给每个客户的库存总量.

当然,我可以运行2个聚合查询来获得我想要的东西,但我只是想知道是否有可能获得具有2组具有1个命令的结果?

预期结果如下:

results:[{
 _id:{
   customer:"some customer"
 },
 orders:[
  {
   year:2011,
   month:1,
   qty:100
  },
   ...
 ]
 deliveries:[
  {
   year:2011,
   month:1,
   qty:50
  },
  {
   year:2011,
   month:2,
   qty:50
  },
  ...
 ]
},...]
Run Code Online (Sandbox Code Playgroud)

小智 9

我遇到了类似的问题,其中我需要将结果分类为多个组,查看所有这些答案让我头晕目眩。经过大量研究后,我找到了我正在寻找的确切东西。

MongoDB 在 3.4 版本中引入了一个名为$facet的新命令,这使得在单个命令中包含多个组变得非常容易。看看他们的文档:

$facet(聚合)

我本来打算在这里用文字解释这一切,但我认为他们的文档更清晰,写得更漂亮,有很好的例子。

希望能帮助到你。


Nei*_*unn 7

您可以在一个查询中执行此操作,您只需要在操作文档时有点创意,然后基本上执行两个 $group阶段,首先按日期添加,然后按客户添加.

因此,首先使用当前的MongoDB版本2.6及更高版本,因为使用了一些运算符:

db.transactions.aggregate([

    // Project an additional array, stands for "order", "delivery"
    { "$project": {
        "_id": 0,
        "customer": 1,
        "order_date": 1,
        "delivery_date": 1,
        "qty": 1,
        "type": { "$literal": ["o","d"] }
    }},

    // Unwind that array, creates two documents by "type"
    { "$unwind": "$type" },

    // Group by "customer", "type" and date
    { "$group": {
        "_id": { 
            "customer": "$customer",
            "type": "$type",
            "year": { 
                "$year": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            },
            "month": { 
                "$month": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            }
        },
        "qty": { "$sum": "$qty" }
    }},

    // Group on the "customer" selecting which array to add to
    { "$group": {
        "_id": "$_id.customer",
        "orders": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "o" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        },
        "deliveries": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "d" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        }
    }},

    // Getting rid of the `false` values in there
    { "$project": {
        "orders": { "$setDifference": [ "$orders", [false] ] },
        "deliveries": { "$setDifference": [ "$deliveries", [false] ] },
    }},

    // But "sets" are not considered ordered, so sort them
    { "$unwind": "$orders" },
    { "$sort": { "orders.year": 1, "orders.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$push": "$orders" },
        "deliveries": { "$first": "$deliveries" }
    }},
    { "$unwind": "$deliveries" },
    { "$sort": { "deliveries.year": 1, "deliveries.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$first": "$orders" },
        "deliveries": { "$push": "$deliveries" }
    }}
)
Run Code Online (Sandbox Code Playgroud)

对于2.6之前的版本,这样做有点不同:

db.transactions.aggregate([

    // Project an additional array, stands for "order", "delivery"
    { "$project": {
        "_id": 0,
        "customer": 1,
        "order_date": 1,
        "delivery_date": 1,
        "qty": 1,
        "type": { "$cond": [ 1, ["o","d"], 0 ] }
    }},

    // Unwind that array, creates two documents by "type"
    { "$unwind": "$type" },

    // Group by "customer", "type" and date
    { "$group": {
        "_id": { 
            "customer": "$customer",
            "type": "$type",
            "year": { 
                "$year": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            },
            "month": { 
                "$month": { 
                    "$cond": [
                        { "$eq": [ "$type", "o" ] },
                        "$order_date",
                        "$delivery_date"
                    ]
                }
            }
        },
        "qty": { "$sum": "$qty" }
    }},

    // Group on the "customer" selecting which array to add to
    { "$group": {
        "_id": "$_id.customer",
        "orders": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "o" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        },
        "deliveries": {
            "$push": {
                "$cond": [
                    { "$eq": [ "$_id.type", "d" ] },
                    {
                        "year": "$_id.year",
                        "month": "$_id.month",
                        "qty": "$qty"
                    },
                    false
                ]
            }
        }
    }},

    // Filter `false` and sort on date
    { "$unwind": "$orders" },
    { "$match": { "orders": { "$ne": false } } },
    { "$sort": { "orders.year": 1, "orders.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$push": "$orders" },
        "deliveries": { "$first": "$deliveries" }
    }},
    { "$unwind": "$deliveries" },
    { "$match": { "deliveries": { "$ne": false } } },
    { "$sort": { "deliveries.year": 1, "deliveries.month": 1 } }, 
    { "$group": {
        "_id": "$_id",
        "orders": { "$first": "$orders" },
        "deliveries": { "$push": "$deliveries" }
    }}

])
Run Code Online (Sandbox Code Playgroud)

基本上总结这里的方法,你正在做的是复制每个文档并分配一个代表"订单"或"交付"的"类型".然后,当您按"客户"和"日期"和"类型"进行分组时,您有条件地根据当前类型决定选择哪个"日期",并只是总结该键下的"数量".

由于结果是每个客户的"订单"和"交付"数组,因此您有条件$push地将该文档值或文档值false取决于文档的当前"类型"对每个数组.

最后,由于这些数组现在包含false所需文档的值以及过滤掉这些值,并确保您的数组按照正确的"日期"顺序排列.

是的,列表有两个以上的$group阶段,繁重的实际上是在两个分组中完成,其他分组只是在那里进行数组操作,如果你需要它,但它给你精确和有序的结果.

所以这可能不是您可能想到的第一种方法,但展示了一些有趣的转换思路,您可以将它们与各种聚合运算符一起使用以解决问题.这是做:)