上代码
db.c3EquipInfo.aggregate([{
//分组聚合 管道
"$group": {
"_id": "$any",
"c3EquipInfo": {
"$push": "$$ROOT"
}
}
}, {
//采用union
"$lookup": {
"from": "supplierDeviceInfo",
"localField": "z",
"foreignField": "z",
"as": "supplierDeviceInfo"
}
}, {
//对两个集合进行合并
"$project": {
"_id": 0,
"allValue": {
"$setUnion": ["$c3EquipInfo", "$supplierDeviceInfo"]
}
}
}, {
//展开
"$unwind": "$allValue"
}, {
"$project": {
//字段映射
"_class": "$allValue._class",
"power": "$allValue.power",
}
}, {
//添加字段
"$addFields": {
"online": {
//判断 if不成立跳过then 进入elese
"$cond": {
"if": {
"$and": ["$lastUpTime", "$interval", {
"$ne": ["$lastUpTime", null]
}, {
"$ne": ["$interval", null]
}]
},
"then": {
// cmd 返回 -1 1 0
"$eq": [ - 1, {
"$cmp": [
{ $subtract: [new Date(), "$lastUpTime"]},
{"$multiply": ["$upInterval", 3000]}
]
}]
},
"else": {
"$cond": {
"if": {
"$and": ["$online", {
"$ne": ["$online", null]
}]
},
"then": "$online",
"else": false
}
}
}
}
}
}, {
//条件
"$match": {
"c3ProjectId": {
"$in": [, 200, 10027, 888]
},
"equipType": 0
}
}])