Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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
MongoDB聚合命令的Java代码_Java_Mongodb_Aggregation Framework_Spring Data Mongodb - Fatal编程技术网

MongoDB聚合命令的Java代码

MongoDB聚合命令的Java代码,java,mongodb,aggregation-framework,spring-data-mongodb,Java,Mongodb,Aggregation Framework,Spring Data Mongodb,我正在尝试使用Java代码的mongoTemplate阅读另一个文档列表的嵌套列表文档,我已经尝试了差不多一天了,但是在做这件事时遇到了困难,请帮我整理一下 我的数据库中的文档是: { "_id" : "DEPT5", "subDepartmentList" : [ { "subDepartmentId" : "SUBDEPT19", "subDepartmentName" : "ABCD", "labServiceList"

我正在尝试使用Java代码的mongoTemplate阅读另一个文档列表的嵌套列表文档,我已经尝试了差不多一天了,但是在做这件事时遇到了困难,请帮我整理一下

我的数据库中的文档是:

{ 
"_id" : "DEPT5",  
"subDepartmentList" : [
    {
        "subDepartmentId" : "SUBDEPT19", 
        "subDepartmentName" : "ABCD",  
        "labServiceList" : [
            {
                "_id" : "123abc", 
                "departmentId" : "DEPT5", 
                "subDepartmentId" : "SUBDEPT19", 
                "labServiceName" : "serviceOne" 
            }, 
            { 
                "_id" : "123def", 
                "departmentId" : "DEPT5", 
                "subDepartmentId" : "SUBDEPT19",  
                "labServiceName" : "hello"
             }
        ]
    }, 
    {
        "subDepartmentId" : "SUBDEPT21", 
        "subDepartmentName" : "IJKL", 
        "labServiceList" : [
            {
                "_id" : "456abc", 
                "departmentId" : "DEPT5", 
                "subDepartmentId" : "SUBDEPT21" 
                "labServiceName" : "hello"                
            }
        ]
    }
]
}

“我的外壳”窗口中的查询如下:

db.labServiceMasters.aggregate([{$unwind: '$subDepartmentList'},
  {$unwind: '$subDepartmentList.labServiceList'},
  {$match: {'subDepartmentList.labServiceList._id': '123def'}},
  {$project: {_id: 0, labService: '$subDepartmentList.labServiceList'}}
])
上面的查询在我的shell窗口中运行良好。我尝试使用下面的Java代码实现相同的查询,但没有成功

DBObject unwind1 = new BasicDBObject("$unwind" , "$subDepartmentList");
unwind1 = new BasicDBObject("$unwind" , "$subDepartmentList.labServiceList");


DBObject match = new BasicDBObject("$match", new BasicDBObject("_id", "123def")); 

DBObject project = new BasicDBObject("$project", new BasicDBObject("_id", 0)
 .append("subDepartmentList.labServiceList", 1));

AggregationOutput output=mongoTemplate.getCollection(MasterCollection).aggregate(unwind1, match, project);
System.out.println("OUTPUT: "+output);
for(DBObject result: output.results()){
    System.out.println("INside for each loop of Results.");
    System.out.println(result);
 }

请帮我解决上述问题。任何建议都是可以接受的。提前谢谢

不一样。您以不同的方式编写了
$match
。您也有“两个”
$unwind
操作,但java代码只定义了一个,并且您用第二个
$unwind
覆盖变量定义。所以我以前问过你类似“你试过什么?”。因为这仍然不是一个代码编写服务。所以“尝试”的意思是做出“合理的努力”。只要注意细节,这里所有的代码问题都很容易解决。@NeilLunn:我以前也试过同样的方法,但没有成功。下面是我在发布这个问题之前写的其他几行。DBObject unwind1=新的BasicDBObject(“$unwind”,“$subDepartmentList”);DBObject unwind2=新的BasicDBObject(“$unwind”,“$subDepartmentList.labServiceList”);最后,我把这两个函数传递给了聚合函数,我刚才告诉你们的是,你们实际上还不够努力。这些都不是问题。它们是基本错误和非常基本的错误。我一眼就认出了他们。好的,谢谢你的建议,我会尽力整理一下,然后回到这里。天哪:-)谢谢尼尔让我自己来做:-)现在可以了,我很高兴你的建议:-)