【问题标题】:Join two collection in mongoDB and extract out data in node js在 mongoDB 中加入两个集合并在节点 js 中提取数据
【发布时间】:2018-08-16 00:20:17
【问题描述】:

我在我的项目中使用 MongoDB 3.6。 我有 2 个集合“用户”和“关注”。我想提取用户关注者和关注者的详细信息(如 Instagram 应用程序)。

用户集合

{
    "id" : "1",
    "name" : "abc",
    "age" : "26"

},
{
    "id" : "2",
    "name" : "xyz",
    "age" : "22"

},
{
    "id" : "3",
    "name" : "qwe",
    "age" : "23"

}

关注收藏

{
    "id" : "2",
    "follow id" : "1"

},
{
    "id" : "3",
    "follow id" : "1"

},
{
    "id" : "1",
    "follow id" : "2"

},
{
    "id" : "2",
    "follow id" : "3"

},
{
    "id" : "1",
    "follow id" : "3"

}

现在我想要下面的 id 2 列表所以 id 2 跟随 id 1 和 id 3 所以,输出应该是这样的

{
    "id" : "1",
    "name" : "abc",
    "age" : "26"

},
{
    "id" : "3",
    "name" : "qwe",
    "age" : "23"

}

为此,我正在使用 $lookup 聚合。但这并没有给出我想要的输出。 这是我的代码-

Follow.aggregate([
    { 
        $lookup:{
            from:"users",
            localField:"id",
            foreignField:"id",
            as:"fromItems"
        }
    },
    {
        $replaceRoot:{newRoot: {$mergeObjects: [ { $arrayElemAt: ["$fromItems", 0 ] }, "$$ROOT" ] } }
    },
    { $project : 
        { 
            fromItems : 0 
        } 
    }
], callback)

更多了解请参考image

【问题讨论】:

    标签: javascript node.js mongodb mean-stack


    【解决方案1】:

    要获取以下 id 2 列表,您可以使用以下查询:

    Follow.aggregate([
        {
            $match: { "id": "2" }
        },
        { 
            $lookup:{
                from:"users",
                localField:"follow id",
                foreignField:"id",
                as:"fromItems"
            }
        },
        {
            $replaceRoot:{newRoot: {$mergeObjects: [ { $arrayElemAt: ["$fromItems", 0 ] }, "$$ROOT" ] } }
        },
        { $project : 
            { 
                id : "$follow id",
                name: 1,
                age: 1 
            } 
        }
    ])
    

    所以这里的重点是idfollow id 之间存在关系,并且在$lookup 阶段之后follow id 成为新的id,因为它是父子关系。

    编辑: 3.4解决方案如下

    Follow.aggregate([
        {
            $match: { "id": "2" }
        },
        { 
            $lookup:{
                from:"users",
                localField:"follow id",
                foreignField:"id",
                as:"fromItems"
            }
        },
        {
            $project: {
                id: "$follow id",
                from: { $arrayElemAt: ["$fromItems", 0 ] }
            }
        },
        { $project : 
            { 
                id : 1,
                name: "$from.name",
                age: "$from.age" 
            } 
        }
    ])
    

    【讨论】:

    • 做到了,但结果不是我想要的。结果是 - {“id”:“2”,“name”:“xyz”,“age”:“22”,“follow id”:“1”} {“id”:“2”,“name”: “xyz”、“年龄”:“22”、“关注 id”:“3”}
    • @RajAmitSingh 你不应该得到follow id,因为我正在使用 $project 将它重命名为id ?您是否运行了完全相同的查询?我收到{ "name" : "abc", "age" : "26", "id" : "1" } { "name" : "qwe", "age" : "23", "id" : "3" }
    • 非常感谢,它对我有用,但现在的问题是 $mergeObjects 没有在 3.4 MongoDB 上运行,是否有 $mergeObjects 的替代解决方案。
    • @RajAmitSingh 我添加了第二个解决方案,它使用 $project 而不是 $mergeObjects
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 2012-09-15
    • 2019-08-02
    • 1970-01-01
    • 2019-10-21
    • 2017-08-10
    相关资源
    最近更新 更多