1、分组求和查询命令
db.getCollection('crm_batch_call').aggregate([
{
$match: {
"call_time":{"$gte":ISODate("2019-08-28T00:00:00.000+0000"),"$lte":ISODate("2019-08-28T15:59:59.000+0000")},//大于小于条件
"call_result_desc" : {$ne:'接通'},//非等于
"gw_id":{$exists:true},//是否存在
"customer_id":{$in:[1,2]}//in操作
"wordstemplate_name":/催告/ //模糊查询
}
},
{
$group: {
_id: {
"call_result_code":"$call_result_code",
"call_result_desc":"$call_result_desc"
},
num:{$sum:1}
}
}
]).forEach(
function(document)
{
print("\r\n"+document._id.call_result_desc+","+document.num)
}
);
2、链表查询
db.getCollection('crm_batch_call').aggregate([
{
$match: {
customer_id:67,
"call_time":{"$gte":ISODate("2019-07-22T00:00:00.000+0000"),"$lte":ISODate("2019-07-24T15:59:59.000+0000")}
}
},
{
$lookup:{
from: "crm_call_record",//连接的表(B表)
localField: "uid",//A表的连接条件
foreignField: "uuid",//B表的连接条件
as: "call_text"//连接结果别名
}
},
{$project:{
"uid":1,
batch_no:1,
phone:1,
wordstemplate_name:1,
robot_name:1,
call_result_desc:1,
intent:1,
call_time:1,
bill_duration:1,
"call_text":{"atext":1,"btext":1,"sort":1
}
}
}
])
3、分组后数据求和
db.getCollection('crm_call_record').aggregate([{
$match: {
"time": {
"$gte": ISODate("2019-01-24" + "T00:00:00.000+0000"),
"$lte": ISODate("2019-01-24" + "T15:59:59.000+0000")
}
}
},
{
$project: {
"uuid": 1
}
},
{
$group: {
_id: "$uuid"
}
},
{
$group: {
_id: null,
count: {
$sum: 1
}
}
}])