node.js - 如何在 MongoDB 中找到嵌套在数组深处的对象?

标签 node.js mongodb

我是 MongoDB 新手,在为我正在开发的新项目设置特定查询时遇到了一些困难。

我有一个如下所示的数据结构(简化版本):

games: {_id: ..., scenes: [{_id: ..., views: [{_id: ...}]}]}

(即游戏包含场景集合,场景包含 View 集合)。

我想在这里查询的是一个特定的 View 对象。我想答案涉及使用 $elemMatch,但我该如何设置呢?经过一番研究+尝试后,我知道我可以这样做来获取场景:

db.collection("games").findOne({
    _id: ObjectId(req.params.gid)}, 
    {
    scenes: {
        $elemMatch: {_id: ObjectId(req.params.sid)}
    }
}...

但是我如何扩展它,以便它只提取我感兴趣的特定 View (通过 _id)?

我想我总能使用 for 循环手动找到我正在寻找的 View 对象,这又带来了另一个问题。关于性能,使用 Mongo 进行这样的查询更好,还是通过拉动整个文档来循环遍历集合来手动进行查询更好?

最佳答案

如果你的集合不大,并且这种操作比较少见,那么使用聚合框架来完成可能没问题。但如果此操作频繁且对性能至关重要,那么我会建议使用应用程序级查询。无论如何,这就是您使用聚合和 $unwind 的方式:

如果这是您的收藏:

> db.col.find().pretty()
{
    "_id" : ObjectId("57e6a5404897ec06f1c3d86f"),
    "games" : [
        {
            "_id" : "game1",
            "scenes" : [
                {
                    "_id" : "scene1",
                    "views" : [
                        {
                            "_id" : "view1"
                        }
                    ]
                }
            ]
        }
    ]
}
{
    "_id" : ObjectId("57e6a5d24897ec06f1c3d870"),
    "games" : [
        {
            "_id" : "game1",
            "scenes" : [
                {
                    "_id" : "scene11",
                    "views" : [
                        {
                            "_id" : "view111"
                        },
                        {
                            "_id" : "view112"
                        },
                        {
                            "_id" : "view113"
                        }
                    ]
                },
                {
                    "_id" : "scene12",
                    "views" : [
                        {
                            "_id" : "view121"
                        }
                    ]
                }
            ]
        },
        {
            "_id" : "game2",
            "scenes" : [
                {
                    "_id" : "scene21",
                    "views" : [
                        {
                            "_id" : "view211"
                        }
                    ]
                }
            ]
        }
    ]
}

假设您想要查找 ID 为“view112”的 View ,您可以执行以下操作:

db.col.aggregate([
    { $unwind: "$games"},
    { $unwind: "$games.scenes"},
    { $unwind: "$games.scenes.views"},
    {
        $match: {
            "games.scenes.views._id": "view112"
        }
    }
])

你会得到:

{
    "_id": ObjectId("57e6a5d24897ec06f1c3d870"),
    "games": {
        "_id": "game1",
        "scenes": {
            "_id": "scene11",
            "views": {
                "_id": "view112"
            }
        }
    }
}

关于node.js - 如何在 MongoDB 中找到嵌套在数组深处的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39677750/

相关文章:

mongodb - 我如何在 Morphia 中反向排序自然?

node.js - 使用 json2csv 创建后如何下载 CSV 文件

node.js - 使用 Node.js 将流分块为数据 block

c++ - nodejs C++ 共享对象

mongodb - Upsert Document和/或添加子文档

Python 数据库调节问题

node.js - 如何根据mongodb中的对象属性删除数组中的对象?

node.js - 如何通过 nodeJS 从 Google Datastore 的索引中排除数组字段?

javascript - Node js : Automated Broadcast, facebook 信使

mongodb - Morphia/MongoDB : ordering search results from advanced queries