我正在使用$lookup
运算符创建mongo聚合查询,但$match
不起作用。
db.alarmActive.aggregate(
[
{
$lookup: {
from: "alarmHistory",
localField: "history_id",
foreignField: "_id",
as: "history"
}
},
{
$unwind: {
path: "$history", preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "alarmTranslation",
localField: "history.plcId",
foreignField: "plcId",
as: "txt"
}
},
{
$unwind: {
path: "$txt", preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
path: "$txt.text", preserveNullAndEmptyArrays: true
}
},
{
$match: {
$and: [
{ "txt.text.language": "de-DE" },
{ "txt.gc_ref": "$gc_ref" } -> doesn't work
//{ "txt.gc_ref": 9 } -> works
]
}
},
{
$lookup: {
from: "infrastructure",
localField: "gc_ref",
foreignField: "Gc_ref",
as: "gc"
}
},
{
$unwind: {
path: "$gc", preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: {
gc_ref: "$gc_ref",
name: "$gc.Name"
},
alarms: {
$push: {
gendate: "$history.gendate",
type: "$txt.type",
location: "$history.location",
text: "$txt.text.text"
}
}
}
}
]).pretty();