微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 MongoDB 中引用集合内的文档

如何解决如何在 MongoDB 中引用集合内的文档

帖子集合中的文档示例

{
    postId: '232323',byUser: 'userid1',post: 'This is my first post',commentsOnPost: [
            {
                commentId: '232323_8888',byUser: 'userid2',comment: 'Congrats',repliesOnPost: [
                    {
                        replyId: '232323_8888_66666',reply: 'Thanks',likesOnReplyBy: ['userid1','userid5','userid3'],},{
                        replyId: '232323_8888_55555',byUser: 'user2',reply: 'Welcome',likesOnReplyBy: ['userid6','userid8','userid9'],}
                ]
            }
        ]
}

我想以这种方式查询这篇文章,以便无论用户 ID 出现在哪里,我都想从匹配的用户 ID 集合中替换它。

[
 {
  userid:userid1,displayName:'Bob',profilePic:'https://example.com/bob_profile.png'
 },{
  userid:userid2,displayName:'Lorish',profilePic:'https://example.com/lorish_profile.png'
 }

.
.
.
.

]

这可能是这样的查询吗?或者我需要在插入文档时做参考吗? 我不想通过执行多次循环来一一替换 userid。

预期产出

{
    postId:'232323',byUser: {
     userid:userid2,profilePic:'https://example.com/lorish_profile.png'
    }
    post:'This is my first post',commentsOnPost:[
        {
            commentId:'232323_8888',byUser: {
             userid:userid1,profilePic:'https://example.com/lorish_profile.png'
            },comment:'Congrats',repliesOnPost:[
                {
                    replyId:'232323_8888_66666',byUser: {
                      userid:userid1,profilePic:'https://example.com/lorish_profile.png'
                    },reply:'Thanks',likesOnReplyBy:[
                    {
                         userid:userid6,displayName:'Kiti',profilePic:'https://example.com/lorish_profile.png'
                     },{
                         userid:userid2,profilePic:'https://example.com/lorish_profile.png'
                     }

                  ],{
                    replyId:'232323_8888_55555',byUser: {
                       userid:userid2,reply:'Welcome',likesOnReplyBy:[
                        {
                         userid:userid5,displayName:'Rahul',profilePic:'https://example.com/lorish_profile.png'
                        }
                    ],}
            ]
        }
    ]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。