我有一个汇总查询,可从3个集合中获取结果。

我正在使用mongoDb 3.4

结果中有一个样本文档。

{
    "_id" : ObjectId("5ba1717ee4b00ce08ca47cfa"),
    "name" : "captain jack",
    "email" : "[email protected]",
    "mobile" : "9000000023",
    "status" : "verified",
    "courses" : [
        {
            "_id" : "13",
            "name" : "Course (03)"
        },{
            "_id" : "12",
            "name" : "Course (03)"
        }
    ],
    "examCompleted" : false,
    "login" : "5ba1717ee4b00fe08ca47cfa",
    "partnerMetaInfo" : {
        "_id" : ObjectId("5ba1717ee4b00fe08ca47cfa"),
        "costCode" : "5761",
        "hub" : "CALCUTTA",
        "location" : "Kolkata"

    }
}


我试图将partnerMetaInfo引入根目录。
我也无法使用_id == 13上的$ match来过滤courses._id

这是我的汇总查询:

db.getCollection("mainCollection").aggregate([
        {
            //Join two collection
            $lookup:{
                from: "Details",
                localField: "username",
                foreignField: "login",
                as: "partnerData"
            }
        },{
            //Limit fields
            $project:{
                "email":1,
                "name":1,
                "mobile":1,
                "status" : 1,
                "courses":"$partnerData.courses",
                "examScore" : "$partnerData.examScore",
                "examCompleted" : "$partnerData.examCompleted",
                "login":"$partnerData.login"
            }
        },
        {
            //Join third collection
            $lookup:{
                from: "PartnerMetaInfo",
                localField: "login",
                foreignField: "partnerId",
                as: "partnerMetaInfo"
            }
        },
        //Remove from partnerData array and place at root level.
        {
            $unwind:
            {
                path: '$courses',
                preserveNullAndEmptyArrays: true
            }
        },{
            $unwind:
            {
                path: '$examScore',
                preserveNullAndEmptyArrays: true
            }
        },{
            $unwind:
            {
                path: '$examCompleted',
                preserveNullAndEmptyArrays: true
            }
        },{
            $unwind:
            {
                path: '$login',
                preserveNullAndEmptyArrays: true
            }
        },//Bring $partnerMetaInfo array to root level.
        {
            $unwind:
            {
                path: '$partnerMetaInfo',
                preserveNullAndEmptyArrays: true
            }
        },{
            $limit:10
        }
    ];


$ unwind之后的partnerMetaInfo最终成为对象。我想将其展平,并将其置于根目录下。

有人可以帮助我吗?

最佳答案

如果您想要得到的只是partnerMetaInfo字段的内容,则可以在管道的末尾添加一个$replaceRoot阶段,如下所示:

{
    $replaceRoot: { "newRoot": { $ifNull: [ "$partnerMetaInfo", {} ] } }
}


否则,如果您只想将partnerMetaInfo字段内的字段移动到根目录,则可以使用$addFields

{
    $addFields: {
        "partnerMetaInfoId" : "$partnerMetaInfo._id",
        "costCode" : "$partnerMetaInfo.costCode",
        "hub" : "$partnerMetaInfo.hub",
        "location" : "$partnerMetaInfo.location"
    }
}


如果您具有动态数量的字段,或者不想对字段名称进行硬编码,则可以使用以下逻辑:

{
    $replaceRoot: { // merge fields of and move them all the way up
        "newRoot": { $mergeObjects: [ "$$ROOT", "$partnerMetaInfo" ] }
    }
}, {
    $project: { // remove the "partnerMetaInfo" field
        "partnerMetaInfo": 0
    }
}

关于node.js - 在mongoDb中将对象放置到根级别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53040711/

10-09 21:57