如何使用mongoTemplate为以下mongo查询编写Java代码?

时间:2017-07-27 10:45:35

标签: java mongodb aggregation-framework spring-data-mongodb

shell窗口中的查询如下:

db.labServiceMasters.aggregate(
  {$unwind: '$subDepartmentList'},
  {$unwind: '$subDepartmentList.labServiceList'},
  {$match: {'subDepartmentList.labServiceList._id': '123def'}},
  {$project: {_id: 1, labServiceList: ['$subDepartmentList.labServiceList']}}
)

我已将上述查询转换为Java代码,如下所示,但我没有在labServiceList中获得结果。另请告诉我如何将结果作为我的POJO对象阅读。

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


DBObject match = new BasicDBObject("$match", new BasicDBObject("subDepartmentList.labServiceList._id", labServiceId)); 

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

AggregationOutput output=mongoTemplate.getCollection(laboratoryMasterCollection).aggregate(unwind1, unwind2, match, project);

List<DBObject> results = (List<DBObject>) output.results();

    for(DBObject response: results){
        System.out.println("INside for each loop of Results");
        Master master=(Master) response;
        System.out.println("master:: "+response.getDepartmentId());
     }

0 个答案:

没有答案