Mongodb聚合按ID和组匹配

时间:2017-04-16 15:55:41

标签: mongodb aggregation-framework

你好我已经在mongodb中创建了一个数据库,并且创建了插入即时通讯,目前正在学习如何使用聚合,我正在努力解决它,并且正在寻找一些帮助,为什么我的命令不会返回任何结果。以下是插入内容:

db.transaction.insert({"EmpId":"005","transaction":[{"TID":"O02","Date":"3/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Steam Gift Card","amount":25}], "Total" : "84.99"});
db.transaction.insert({"EmpId":"001","transaction":[{"TID":"O03","Date":"3/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Playstation Controller","amount":25}], "Total" : "84.99"});
db.transaction.insert({"EmpId":"003","transaction":[{"TID":"O04","Date":"3/27/2017","product":[{"product_name":"Unchartered 2","quantity":1,"amount":49.99}]}], "Total" : "49.99"});
db.transaction.insert({"EmpId":"003","transaction":[{"TID":"O05","Date":"3/27/2017","product":[{"product_name":"Playstation 4","quantity":1,"amount":359.99}]},{"product_name":"Battlefield 2","amount":49.99}], "Total" : "409.98"});
db.transaction.insert({"EmpId":"004","transaction":[{"TID":"O06","Date":"4/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Steam Gift Card","amount":50}], "Total" : "109.99"});
db.transaction.insert({"EmpId":"001","transaction":[{"TID":"O07","Date":"4/27/2017","product":[{"product_name":"CSGO","quantity":1,"amount":13.99}]},{"product_name":"GTA4","amount": 39.99}], "Total" : "53.98"});
db.transaction.insert({"EmpId":"002","transaction":[{"TID":"O08","Date":"5/27/2017","product":[{"product_name":"Sparta","quantity":3,"amount":17.99}]},{"product_name":"Action Figures","amount": 9.99}], "Total" : "63.96"});
db.transaction.insert({"EmpId":"005","transaction":[{"TID":"O09","Date":"3/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Steam Gift Card","amount":25}], "Total" : "84.99"});
db.transaction.insert({"EmpId":"002","transaction":[{"TID":"O10","Date":"5/27/2017","product":[{"product_name":"Playstation Headset","quantity":3,"amount":29.99}]},{"product_name":"GTA 4","amount": 39.99},{"product_name":"Unchartered 2","amount": 49.99}], "Total" : "119.97"});
db.transaction.insert({"EmpId":"004","transaction":[{"TID":"O11","Date":"5/27/2017","product":[{"product_name":"Harry Potter","quantity":2,"amount":12.99}]},{"product_name":"Lord Of The Ring's","amount": 9.99}], "Total" : "35.97"});

这是我的问题:

db.transaction.aggregate([{ $match: {EmpId: "001"}}, { $group: {_id: "EmpId", total: {$sum: "Total"}}}])

结果如下:

{ "_id" : "EmpId", "total" : 0 }

1 个答案:

答案 0 :(得分:1)

首先请将“Total”值设为数字而不是字符串,否则sum将不起作用。

db.transaction.insert({"EmpId":"005","transaction":[{"TID":"O02","Date":"3/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Steam Gift Card","amount":25}], "Total" : 84.99});
db.transaction.insert({"EmpId":"001","transaction":[{"TID":"O03","Date":"3/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Playstation Controller","amount":25}], "Total" : 84.99});
db.transaction.insert({"EmpId":"003","transaction":[{"TID":"O04","Date":"3/27/2017","product":[{"product_name":"Unchartered 2","quantity":1,"amount":49.99}]}], "Total" : 49.99});
db.transaction.insert({"EmpId":"003","transaction":[{"TID":"O05","Date":"3/27/2017","product":[{"product_name":"Playstation 4","quantity":1,"amount":359.99}]},{"product_name":"Battlefield 2","amount":49.99}], "Total" : 409.98});
db.transaction.insert({"EmpId":"004","transaction":[{"TID":"O06","Date":"4/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Steam Gift Card","amount":50}], "Total" : 109.99});
db.transaction.insert({"EmpId":"001","transaction":[{"TID":"O07","Date":"4/27/2017","product":[{"product_name":"CSGO","quantity":1,"amount":13.99}]},{"product_name":"GTA4","amount": 39.99}], "Total" : 53.98});
db.transaction.insert({"EmpId":"002","transaction":[{"TID":"O08","Date":"5/27/2017","product":[{"product_name":"Sparta","quantity":3,"amount":17.99}]},{"product_name":"Action Figures","amount": 9.99}], "Total" : 63.96});
db.transaction.insert({"EmpId":"005","transaction":[{"TID":"O09","Date":"3/27/2017","product":[{"product_name":"COD","quantity":1,"amount":59.99}]},{"product_name":"Steam Gift Card","amount":25}], "Total" : 84.99});
db.transaction.insert({"EmpId":"002","transaction":[{"TID":"O10","Date":"5/27/2017","product":[{"product_name":"Playstation Headset","quantity":3,"amount":29.99}]},{"product_name":"GTA 4","amount": 39.99},{"product_name":"Unchartered 2","amount": 49.99}], "Total" : 119.97});
db.transaction.insert({"EmpId":"004","transaction":[{"TID":"O11","Date":"5/27/2017","product":[{"product_name":"Harry Potter","quantity":2,"amount":12.99}]},{"product_name":"Lord Of The Ring's","amount": 9.99}], "Total" : 35.97});

在字段名称之前使用'$'符号代替组查询

db.transaction.aggregate([{ $match: {"EmpId": "001"}}, { $group: {_id: "$EmpId", total: {$sum: "$Total"}}}])

结果如下:

{
    "_id" : "001",
    "total" : 138.97
}