MongoDB 3.4 - 获取没有根文档的子文档数组

时间:2017-02-11 12:44:27

标签: arrays mongodb field subdocument

我有一个projects集合,其中包含以下文档:

{
        "_id" : ObjectId("589eff3fee3d13019843f55a"),
        "name" : "Project A",
        "desc" : "test",
        "numofvms" : 0,
        "templates" : [
                {
                        "_id" : ObjectId("589e4c14ee3d131bac8b403c")
                },
                {
                        "_id" : ObjectId("589e4c1dee3d131bac8b403d")
                }
        ],
        "nodes" : [
                {
                        "_id" : ObjectId("589eff8f2bb59057c3f9b89d"),
                        "name" : "Node A"
                },
                {
                        "_id" : ObjectId("589eff962bb59057c3f9b89e"),
                        "name" : "Node B"
                },
                {
                        "_id" : ObjectId("589eff982bb59057c3f9b89f"),
                        "name" : "Node C"
                },
                {
                        "_id" : ObjectId("589eff9a2bb59057c3f9b8a0"),
                        "name" : "Node D"
                }
        ],
        "links" : [ ]
}

我尝试接收给定文档(项目)的子文档数组(nodes字段),但没有根文档。结果应如下所示:

[
        {
                "_id" : ObjectId("589eff8f2bb59057c3f9b89d"),
                "name" : "Node A"
        },
        {
                "_id" : ObjectId("589eff962bb59057c3f9b89e"),
                "name" : "Node B"
        },
        {
                "_id" : ObjectId("589eff982bb59057c3f9b89f"),
                "name" : "Node C"
        },
        {
                "_id" : ObjectId("589eff9a2bb59057c3f9b8a0"),
                "name" : "Node D"
        }
]

我尝试了不同的方法,如下面的方法,但无法摆脱根文档:

> db.projects.find({_id: ObjectId("589eff3fee3d13019843f55a")}, { "nodes": 1, _id: 0 }).pretty()
{
        "nodes" : [
                {
                        "_id" : ObjectId("589eff8f2bb59057c3f9b89d"),
                        "name" : "Node A"
                },
                {
                        "_id" : ObjectId("589eff962bb59057c3f9b89e"),
                        "name" : "Node B"
                },
                {
                        "_id" : ObjectId("589eff982bb59057c3f9b89f"),
                        "name" : "Node C"
                },
                {
                        "_id" : ObjectId("589eff9a2bb59057c3f9b8a0"),
                        "name" : "Node D"
                }
        ]
}

这可以直接在查询中实现,还是必须在我的应用程序中手动提取数组?

2 个答案:

答案 0 :(得分:2)

试试这个:

db.collection.aggregate([
    {
        $unwind: '$nodes'
    },
    {
        $match: {_id: ObjectId("589eff3fee3d13019843f55a") }
    },
    {
        $replaceRoot: { newRoot: "$nodes" }
    }
]).toArray();

你会得到

[
    {
        "_id" : ObjectId("589eff8f2bb59057c3f9b89d"),
        "name" : "Node A"
    },
    {
        "_id" : ObjectId("589eff962bb59057c3f9b89e"),
        "name" : "Node B"
    },
    {
        "_id" : ObjectId("589eff982bb59057c3f9b89f"),
        "name" : "Node C"
    },
    {
        "_id" : ObjectId("589eff9a2bb59057c3f9b8a0"),
        "name" : "Node D"
    }
]

$unwind运算符将从输入文档中解构nodes字段,以输出每个元素的文档。

然后使用$replaceRoot阶段将nodes文档提升到最高级别,放弃当前的顶级字段。

希望这有帮助。

答案 1 :(得分:1)

以下是使用聚合(Mongo v3.4 +)执行此操作的方法:

db.projects.aggregate([
	{$match: {_id: ObjectId("589eff3fee3d13019843f55a")}},
	{$unwind: '$nodes'},
	{$replaceRoot: { newRoot: "$nodes"}}
]).toArray();

查看the docs