在mongoDb中将对象放置到根级别

时间:2018-10-29 07:30:26

标签: node.js mongodb mongodb-query aggregation-framework

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

我正在使用mongoDb 3.4

结果中有一个样本文档。

{ 
    "_id" : ObjectId("5ba1717ee4b00ce08ca47cfa"),  
    "name" : "captain jack", 
    "email" : "jack@gmail.com", 
    "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来过滤course._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最终成为对象。我想将其展平,并将其置于根目录下。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

如果要获得的全部结果就是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
    }
}