我的藏品中有以下文件:
{ "_id" : ObjectId("5785e5649b732ab238cfc519"), "name" : "Apple", "category" : "Fruit", "price" : 100, "discount" : 5 }
{ "_id" : ObjectId("5785e5709b732ab238cfc51a"), "name" : "Orange", "category" : "Fruit", "price" : 90, "discount" : 5 }
{ "_id" : ObjectId("5785e5819b732ab238cfc51b"), "name" : "PineApple", "category" : "Fruit", "price" : 60, "discount" : 2 }
{ "_id" : ObjectId("5785e5969b732ab238cfc51c"), "name" : "Potatto", "category" : "Vegetable", "price" : 10, "discount" : 1 }
{ "_id" : ObjectId("5785e5c39b732ab238cfc51d"), "name" : "Cabbage", "category" : "Vegetable", "price" : 5, "discount" : 1 }
和预期结果
{ "_id" : { "category" : "Vegetable" }, "total" : 15 }
我正在使用mongoDB查询来查找带有蔬菜类别的总数,如下所示
db.stall.aggregate([{$group: {_id: {category: "Vegetable" }, total: {$sum: "$price"}}}]);
但我得到以下结果
{ "_id" : { "category" : "Vegetable" }, "total" : 265 }
如何找到蔬菜类别的总列数和折扣列的总和。
答案 0 :(得分:3)
我不确定我的问题是否正确,但这会过滤价格和蔬菜类别折扣总和。
array() {
[74]=> int(1467295200)
[75]=> int(1467381600)
[76]=> int(1467468000)
[77]=> int(1467554400)
[48]=> int(1467639000)
[78]=> int(1467640800)
[79]=> int(1467727200)
[36]=> int(1468243800)
[49]=> int(1468243800)
[30]=> int(1468285200)
[24]=> int(1468287000)
[18]=> int(1468288800)
[12]=> int(1468290600)
[6]=> int(1468292400)
[37]=> int(1468330200)
[31]=> int(1468458000)
[25]=> int(1468459800)
[19]=> int(1468461600)
[13]=> int(1468463400)
[7]=> int(1468465200)
[38]=> int(1468503000)
[32]=> int(1468544400)
[26]=> int(1468546200)
[20]=> int(1468548000)
[14]=> int(1468549800)
[8]=> int(1468551600)
[39]=> int(1468589400)
}