在mongodb中聚合内部文档

时间:2014-01-30 13:25:16

标签: mongodb

我有文件销售

db.sale.findOne({_id : ObjectId("52ea4dd29dbc7923107ddb97")})

{
    "_id" : ObjectId("52ea4dd29dbc7923107ddb97"),
    "firm" : ObjectId("52e56c009dbc794999ea5c3d"),
    "patient" : {
        "last" : "",
        "first" : ""
    },
    "doc" : "",
    "hospital" : "",
    "discount" : 0,
    "dd" : "",
    "mode" : "cash",
    "invoice" : "300114-undefined-1",
    "items" : [
        {
            "bat" : "BJFE",
            "narco" : 0,
            "name" : "GDRNCCD",
            "mrp" : 1,
            "free" : 0,
            "qty" : 1,
            "item_discount" : 0,
            "wpr" : 1,
            "exp" : "1425168000000"
        },
        {
            "bat" : "",
            "narco" : 0,
            "name" : "GDRN vbhjdsfb",
            "mrp" : 1,
            "free" : 0,
            "qty" : 1,
            "item_discount" : 0,
            "wpr" : 0,
            "exp" : "[object Object]"
        },
        {
            "bat" : "",
            "narco" : 0,
            "name" : "GDRN vbhjdsfb",
            "mrp" : 1,
            "free" : 0,
            "qty" : 1,
            "item_discount" : 0,
            "wpr" : 0,
            "exp" : "[object Object]"
        }
    ],
    "date" : ISODate("2014-01-30T00:00:00Z"),
    "mob" : "",
    "email" : ""
}

如果一个字段中的项目汇总总数,并将所有项目的mrp * qty汇总到一个字段中。 我已经阅读了mognodb聚合,但它只聚合在不在单个文档中的匹配文档组中。可能吗?

{
    "_id" : ObjectId("52ea4dd29dbc7923107ddb97"),
    "firm" : ObjectId("52e56c009dbc794999ea5c3d"),
     'total_items' : items.length,
      "total" :     mrp*qty of all items,
}

2 个答案:

答案 0 :(得分:1)

db.sales.aggregate( 
    {$unwind: "$items"},
    {$project: {_id: 1,firm:1, total: {$multiply: ["$items.mrp", "$items.qty"]}}},
    {$group: {_id: {"id":"$_id", firm:"$firm"}, count: {$sum:1} , total : {$sum:"$total"}}}
)

稍作修改:_id包含idfirm,您需要额外的投影来匹配您想要的文档,但我认为这不重要。

另外,您可以轻松地更改为按农场分组

答案 1 :(得分:0)

感谢Orid,我试过了,

db.sale.aggregate(

{ $match :{ firm :ObjectId("52e56c009dbc794999ea5c3d") }  }, 

{$project : {day : {$dayOfMonth : '$date'},items :1,patient :1,invoice :1}}, 

{$match : {day: {$gte : new Date().getDate()}}},

 {$unwind : "$items"}, 

{$project: {_id: 1,patient:1,invoice :1, total: {$multiply: ["$items.mrp", "$items.qty"]}}},  

{$group: {_id: {"id":"$_id", invoice:"$invoice",patient :"$patient"}, count: {$sum:1} , total : {$sum:"$total"}}})

<强>结果

{
    "result" : [
        {
            "_id" : {
                "id" : ObjectId("52eab6129dbc7923107ddbaf"),
                "invoice" : "310114-undefined-1",
                "patient" : {
                    "last" : "",
                    "first" : ""
                }
            },
            "count" : 2,
            "total" : 25
        },
        {
            "_id" : {
                "id" : ObjectId("52eab6129dbc7923107ddbb0"),
                "invoice" : "310114-undefined-1",
                "patient" : {
                    "last" : "",
                    "first" : ""
                }
            },
            "count" : 1,
            "total" : 1
        },
        {
            "_id" : {
                "id" : ObjectId("52eab6129dbc7923107ddbae"),
                "invoice" : "310114-undefined-1",
                "patient" : {
                    "last" : "",
                    "first" : ""
                }
            },
            "count" : 2,
            "total" : 5
        }
    ],
    "ok" : 1
}