node.js - MongoDB $lookup 结果作为对象而不是数组

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

表“用户”:

{
    "_id" : ObjectId("573b09e6322304d5e7c6256e"),
    "name" : "John",
    "age" : 30,
    "list" : [ 
        "userId1", 
        "userId2", 
        "userId3", 
        "userId2"
    ]
},
{
    "_id" : "userId1",
    "name" : "Derek",
    "age" : 34
},
{
    "_id" : "userId2",
    "name" : "Homer",
    "age" : 44
},
{
    "_id" : "userId3",
    "name" : "Bobby",
    "age" : 12
}

我的查询:

db.user.aggregate(
    [
        {
            $match: { "_id": ObjectId("573b09e6322304d5e7c6256e") }
        },
        {
            $lookup:
            {
                from: "user",
                localField: "list",
                foreignField: "_id",
                as: "listData"
            }
        }
    ]
)

我需要通过 id 查找一个用户,并将连接列表数据作为 id/value 对象:

{
    "_id" : ObjectId("573b09e6322304d5e7c6256e"),
    "name" : "John",
    "age" : 30,
    "list" : [ 
        "userId1", 
        "userId2", 
        "userId3", 
        "userId2"
    ],
    "listData" : {
        "userId1": {
            "_id" : "userId1",
            "name" : "Derek",
            "age" : 34
        }, 
        "userId2": {
            "_id" : "userId2",
            "name" : "Homer",
            "age" : 44
        }, 
        "userId3": {
            "_id" : "userId3",
            "name" : "Bobby",
            "age" : 12
        }
    ]
}

但是我得到了 listData 作为数组:

{
    "_id" : ObjectId("573b09e6322304d5e7c6256e"),
    "name" : "John",
    "age" : 30,
    "list" : [ 
        "userId1", 
        "userId2", 
        "userId3", 
        "userId2"
    ],
    "listData" : [ 
        {
            "_id" : "userId1",
            "name" : "Derek",
            "age" : 34
        }, 
        {
            "_id" : "userId2",
            "name" : "Homer",
            "age" : 44
        }, 
        {
            "_id" : "userId3",
            "name" : "Bobby",
            "age" : 12
        }
    ]
}

我需要更改什么才能将数组作为对象并以键作为 id?数据库版本v4.0.2

最佳答案

您可以使用$addFieldslistData 转换为对象。在此之前,您应该使用 $map将数组转换为 k-v 对数组,然后可以使用 $arrayToObjec要动态生成新 key ,请尝试:

db.col.aggregate([
    {
            $match: { "_id": ObjectId("573b09e6322304d5e7c6256e") }
    },
    {
        $lookup:
        {
            from: "user",
            localField: "list",
            foreignField: "_id",
            as: "listData"
        }
    },
    {
        $addFields: {
            listData: {
                $arrayToObject: {
                    $map: {
                        input: "$listData",
                        as: "user",
                        in: {
                            k: "$$user._id",
                            v: "$$user"
                        }
                    }
                }
            }
        }
    }
])

关于node.js - MongoDB $lookup 结果作为对象而不是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901893/

相关文章:

node.js - 使用 populate 查询两个模式

MongoDB 查询在集合中找不到文档

mongodb - 有没有办法比较 MongoDB 查询中的两个字段

java - 如何在mongodb中存储数组?

javascript - MongoDB - 两个多边形的地理空间交集

regex - "begins with"在数字数组中查询的 Mongo 匹配数字

mysql - Windows下NodeJS如何使用db-mysql扩展?

javascript - 比较用 PHP hash() 和 NodeJS crypto.createHash() 制作的 SHA256

node.js - Google Cloud Endpoints 客户端库 NodeJS

node.js - 未处理的PromiseRejection警告: Insufficient funds