【问题标题】:MongoDB | How do I merge with an existing value?MongoDB |如何与现有值合并?
【发布时间】:2019-03-19 13:55:16
【问题描述】:

DB如下。


收藏:评论

{
    "_id" : ObjectId("5bc4348f8e798ccb030991e8")
    "comment_no" : 143,
    “comment_content” : “test test”
}


收藏:发布

{
    "_id" : ObjectId("5bc16b068e798ccb03096efa"),
    "post_no" : 48,
    "comment" : [ 
        {
            "comment_no" : 143,
            "comment_group" : 1 // This value will disappear.
        }
    ]
}


查询如下所示:

db.getCollection('post').aggregate([
{
    $match : {post_no: 48}
    },
    {
        $lookup: {
            from: 'comment'
            localField: 'comment.comment_no',
            foreignField: 'comment_no',
            as: 'comment'
        }
    }
])


结果如下。

{
    "_id" : ObjectId("5bc16b068e798ccb03096efa"),
    "post_no" : 48,
    "comment" : [ 
        {
            "comment_no" : 143,
            “comment_content” : “test test”
        }
    ]
}


现有数据消失,但我想合并comment_group的值。

例如,我想要的结果是..

{
    "_id" : ObjectId("5bc16b068e798ccb03096efa"),
    "post_no" : 48,
    "comment" : [ 
        {
            "comment_no" : 143,
            “comment_content” : “test test”,
            "comment_group" : 1 // Here!! I want to use this value.
        }
    ]
}

我可以进行查询,以便将值合并到 comment_no 中吗?

【问题讨论】:

    标签: mongodb aggregation-framework lookup


    【解决方案1】:

    你可以试试下面的聚合

    您需要首先 $unwind comment 数组与确切的 comment_no 匹配,然后 $group 再次回滚到其原始形式

    db.getCollection("post").aggregate([
      { "$match": { "post_no": 48 }},
      { "$unwind": "$comment" },
      { "$lookup": {
        "from": "comment"
        "localField": "comment.comment_no",
        "foreignField": "comment_no",
        "as": "comment.comment_content"
      }},
      { "$unwind": "$comment.comment_content" },
      { "$addFields": {
        "comment": {
          "$mergeObjects": ["$comment", "$comment.comment_content"]
        }
      }},
      { "$group": {
        "_id": "$_id",
        "comment": { "$push": "$comment" }
      }}
    ])
    

    【讨论】:

    • 谢谢!!评论集中其实有很多字段。除了 'addfields' 之外,还有没有办法组合字段?
    • 非常好!!!非常感谢!!它真的帮了我很多。但实际上“Post”集合中有很多字段。为什么“帖子集合”中的所有值都消失了?
    • $first 用于$group 阶段中​​的其他字段。 stackoverflow.com/questions/52215916/first-in-mongodb/…
    • @flyhigh 你找到我了吗?
    • @Anthony_Winzlet { "$group": { "_id": "$_id", ** "post_no": { "$first": "$post_no" }, ** "comment": { "$push": "$comment" } }} 对吗?我希望它是简单而动态的,但我认为我应该使用这种方法。谢谢!!!
    【解决方案2】:

    你可以像这样得到你想要的:

    db.post.aggregate([
    {
        $match : { post_no: 48 }
    }, {
        $unwind: '$comment' // flatten 'comment' array into sub-documents
    }, {
        $lookup: {
            from: 'comment',
            let: {
                'comment': '$comment', // keep a reference to the 'comment' sub-document
            },
            pipeline: [{
                $match: { $expr: { $eq: [ '$comment_no', '$$comment.comment_no' ] } } // perform the join
            }, {
                $replaceRoot: { "newRoot": { $mergeObjects: [ '$$comment', '$$ROOT' ] } } // merge the found object with the original content of 'comment'
            }],
            as: 'comment'
        }
    }, {
        $group: { // restore the original structure
            _id: '$_id',
            'comment': { $push: { $arrayElemAt: [ '$comment', 0 ] }  }
        }
    }])
    

    【讨论】:

    • 这个方法返回的结果是数组中的对象。它有帮助。谢谢你。注释(数组) - 数组 - 对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    相关资源
    最近更新 更多