Mongodb聚合匹配不起作用

时间:2017-08-01 11:04:22

标签: mongodb mongoose mongodb-query match aggregation-framework

我正在使用$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();

0 个答案:

没有答案