我有一个db数据如下
SELECT * FROM PRODUCTS
WHERE CONCAT(code, ' ',description) LIKE %your-string%
我想要一个聚合传输输出如下
{date:" 2017-12-01T00:00:0Z",total:" 2",已完成:1,失败:1}
我已尝试过此代码,但未按上述方式生成结果
{
"_id" : ObjectId("5a2109572222085be93ef10d"),
"name" : "data1",
"date" : "2017-12-01T00:00.0Z",
"status" : "COMPLETED"},{
"_id" : ObjectId("5a2109572222085be93ef10d"),
"name" : "data1",
"date" : "2017-12-01T00:00.0Z",
"status" : "FAILED"}
答案 0 :(得分:8)
db.test.aggregate(
// Pipeline
[
// Stage 1
{
$group: {
_id:"$date",
total:{$sum:1},
failed:{$sum:{$cond:[{$eq:["$status","FAILED"]},1,0]}},
completed:{$sum:{$cond:[{$eq:["$status","COMPLETED"]},1,0]}}
}
},
// Stage 2
{
$project: {
date : '$_id',
total : 1,
failed : 1,
completed : 1,
_id : 0,
}
},
]
);