MongoDB使用$ sum聚合$ match和$ group

时间:2014-05-27 08:58:58

标签: mongodb aggregation-framework

我有一个包含这样文件的集合:

{
    "Company" : "4433",
    "Descripcion" : "trabajo",
    "Referencia" : "11817",
    "HoraImportado" : "15:54",
    "ImportedOd" : "2014-05-20T13:54:28.493Z",
    "Items" : [],
    "Notes" : [ 
        {
            "_id" : ObjectId("537b5ea4c61b1d1743f43420"),
            "NoteDateTime" : "2014-05-20T13:54:44.418Z",
            "Description" : "nota",
            "IsForTechnician" : true,
            "Username" : "admin"
        }, 
        {
            "_id" : ObjectId("537c4a549e956f77ab8c7c38"),
            "NoteDateTime" : ISODate("2014-05-21T06:40:20.299Z"),
            "Description" : "ok",
            "IsForTechnician" : true,
            "Username" : "admin"
        }
    ],
    "OrderState" : "Review",
    "SiniestroDe" : "Emergencia",
    "Technicians" : [ 
        {
            "TechnicianId" : ObjectId("53465f9d519c94680327965d"),
            "Name" : "Administrator",
            "AssignedOn" : ISODate("2014-05-20T13:54:44.373Z"),
            "RemovedOn" : null
        }
    ],
    "TechniciansHistory" : [ 
        {
            "TechnicianId" : ObjectId("53465f9d519c94680327965d"),
            "Name" : "Administrator",
            "AssignedOn" : ISODate("2014-05-20T13:54:44.373Z"),
            "RemovedOn" : null
        }, 
        {
            "Name" : "Nuevo",
            "AssignedOn" : ISODate("2014-05-20T13:54:44.373Z"),
            "RemovedOn" : null,
            "TechnicianId" : ObjectId("5383577a994be8b9a9e3f01e")
        }
    ],
    "Telefonos" : "615554006",
    "_id" : ObjectId("537b5ea4c61b1d1743f4341f"),
    "works" : [ 
        {
            "code" : "A001",
            "name" : "Cambiar bombilla",
            "orderId" : "537b5ea4c61b1d1743f4341f",
            "price" : "11",
            "ID" : 33,
            "lazyLoaded" : true,
            "status" : 0,
            "Date" : ISODate("2014-05-21T06:40:20.299Z"),
            "TechnicianId" : "53465f9d519c94680327965d",
            "_id" : ObjectId("537c4a549e956f77ab8c7c39")
        }, 
        {
            "code" : "A001",
            "name" : "Cambiar bombilla",
            "orderId" : "537b5ea4c61b1d1743f4341f",
            "price" : "11",
            "ID" : 34,
            "lazyLoaded" : true,
            "status" : 0,
            "Date" : ISODate("2014-05-21T06:40:20.299Z"),
            "TechnicianId" : "53465f9d519c94680327965d",
            "_id" : ObjectId("537c4a549e956f77ab8c7c3a")
        }
    ]
}

现在,我希望获得所选TechnicianId数组的工作,按TechnicianId分组,并获得每位技术人员的工作价格总和。+

我试着用这个:

db.orders.aggregate([
                     { $match: { 'works.TechnicianId': {$in:['53465f9d519c94680327965d']}}},
                     { $group: { _id: "$works.TechnicianId",total:{$sum:'$works.price'}}},
                   ])

这就是结果:

{
    "result" : [ 
        {
            "_id" : [ 
                "53465f9d519c94680327965d", 
                "53465f9d519c94680327965d"
            ],
            "total" : 0
        }
    ],
    "ok" : 1
}

总和是$ sum但是0但应该是44。

3 个答案:

答案 0 :(得分:2)

尝试添加展开,

db.orders.aggregate([
                 { $match: { 'works.TechnicianId': {$in:['53465f9d519c94680327965d']}}},
                 { $unwind: "$works" },
                 { $group: { _id: "$works.TechnicianId",total:{$sum:'$works.price'}}},
               ])

点击此处了解更多信息:http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/

答案 1 :(得分:0)

price值是一个字符串。 $sum仅适用于Numbers。

我通过运行以下内容检查了这一点:

db.foo.insert({"cost": "1"})
db.foo.insert({"cost": "2"})
db.foo.insert({"cost": "3"})
db.foo.insert({"cost": 4})
db.foo.insert({"cost": 5})
db.foo.aggregate([{$group: {_id: null, cost: {$sum: "$cost"}}}])
{ "result" : [ { "_id" : null, "cost" : 9 } ], "ok" : 1 }

According to this answer,您无法在普通的Mongo查询中投放值,因此您无法将字符串更改为内联数字。

您应该将所有值更新为Number数据类型或使用map-reduce。我去找前者。

如果值是一个字符串以防止出现浮点错误,请考虑乘以100以将值存储为美分:"10.50" --> 1050


作为Lalit Agarwal indicated,您还需要unwind一系列作品。如果你不知道会发生什么:

db.bar.insert({"works": [{price: 10}]})
db.bar.insert({"works": [{price: 20}, {price: 30}]})
db.bar.insert({"works": [{price: 40}, {price: 50}]})

db.bar.aggregate([ 
    {$group: {_id: null, total: {$sum: "$works.price"} }}
])
{ "result" : [ { "_id" : null, "total" : 0 } ], "ok" : 1 }

db.bar.aggregate([
    {$unwind: "$works"}, 
    {$group: {_id: null, total: {$sum: "$works.price"} }}
])
{ "result" : [ { "_id" : null, "total" : 150 } ], "ok" : 1 }

$unwind做的是在初始3中创建5个文档,所有文档都在works字段中有一个值。然后它将它们分组并加以总结。

答案 2 :(得分:-1)

head(lapply(x, function(x) attributes(x)$label))
#    $SEQN
#[1] "Respondent sequence number"

#$SDDSRVYR
#[1] "Data release cycle"

#$RIDSTATR
#[1] "Interview/Examination status"

#$RIAGENDR
#[1] "Gender"

#$RIDAGEYR
#[1] "Age in years at screening"

#$RIDAGEMN
#[1] "Age in months at screening - 0 to 24 mos"
相关问题