Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的MongoDB,如何从另一个文档中包含的数组中检索嵌套文档?_Java_Mongodb - Fatal编程技术网

Java中的MongoDB,如何从另一个文档中包含的数组中检索嵌套文档?

Java中的MongoDB,如何从另一个文档中包含的数组中检索嵌套文档?,java,mongodb,Java,Mongodb,如何从图像中所示的电影文档中的嵌套阵列中检索不同的演员。下面的代码返回演员出现在其中的每部电影的完整文档,到目前为止,我尝试过的都不起作用 谢谢 MongoCursor test = filmCollection.find(Filters.and( Filters.eq("Actors.First name", Pattern.compile("^"+firstName+".*")), Filters.eq("Actors.Last name", Pattern.compile("^"+last

如何从图像中所示的电影文档中的嵌套阵列中检索不同的演员。下面的代码返回演员出现在其中的每部电影的完整文档,到目前为止,我尝试过的都不起作用

谢谢

MongoCursor test = filmCollection.find(Filters.and(
Filters.eq("Actors.First name", Pattern.compile("^"+firstName+".*")),
Filters.eq("Actors.Last name", Pattern.compile("^"+lastName+".*")))).iterator();

首先用unwind解包所有嵌套数组,只匹配目标名称,最后分组

db.films.aggregate([{
    $unwind: "$Actors"

}, {
    $match: {
        "Actors.First name": {$regex: "^name.*"}
    }
}, {
    $group: {
        _id: "$Actors.actorId",
        "first": {
            $first: "$Actors.First name"
        },
        "last": {
            $first: "$Actors.Last name"
        }
    }
}])

您可以使用或使用聚合的。有相当于Java驱动程序的API。谢谢!用$group解决了这个问题