在现有字段上使用$ sum会返回值0

时间:2017-02-09 12:55:44

标签: mongodb mongodb-query

我必须在category的基础上总结subOrderAmt.Below是样本。

{
    "_id" : ObjectId("589b2214a8c4d36ab9f3a7ec"),
    "_class" : "com.homeshop18.clickstream.mongo.domain.widget.Order",
    "orderId" : NumberLong(946588817),
    "subOrders" : [
        {
            "subOrderId" : NumberLong("41357348772"),
            "productId" : NumberLong(32164935),
            "subOrderAmt" : "220.00",
            "category" : NumberLong(3043),
            "isCancelled" : false
        },
        {
            "subOrderId" : NumberLong("41357348773"),
            "productId" : NumberLong(33173277),
            "subOrderAmt" : "200.00",
            "category" : NumberLong(3043),
            "isCancelled" : false
        },
        {
            "subOrderId" : NumberLong("41357348774"),
            "productId" : NumberLong(32852897),
            "subOrderAmt" : "201.00",
            "category" : NumberLong(3042),
            "isCancelled" : false
        }
    ],
    "bannerUserId" : "abdb1ec23721832dafa3e5535ff5295c75ad6f8cd2c1c91093b9ad7ff65de8ff"
}

sqlDB查询: -     SELECT sum(subOrderAmt)as totalAmt,category     FROM(order.subOrders)     GROUP BY`category

mongoDB查询

  

db.order.aggregate([{$ unwind:“$ subOrders”},{$ group:{_ id:{category:“$ subOrders.category”},totalAmt:{$ sum:“$ subOrders.subOrderAmt” },count:{$ sum:1}}}])       {“_ id”:{“category”:NumberLong(3042)},“totalAmt”:0,“count”:1}       {“_ id”:{“category”:NumberLong(3043)},“totalAmt”:0,“count”:8}

无法获得subOrderAmt的总和。请帮助。

0 个答案:

没有答案