如何获取文档的另一个嵌套列表的嵌套记录?

How to fetch the nested record of another nested list of a document?

我在检索另一个嵌套列表的嵌套文档对象时遇到困难。请帮我解决同样的问题。我的mongoDB文档如下:

{ 
"_id" : "PT5", 
"departmentId" : "DEPT5", 
"subDepartmentList" : [
    {
        "subDepartmentId" : "SUBDEPT19", 
        "subDepartmentName" : "X-Ray",  
        "labServiceList" : [
            {
                "_id" : "123abc", 
                "subDepartmentId" : "SUBDEPT19", 
                "labServiceName" : "serviceOne"
            }, 
            { 
                "_id" : "123def", 
                "subDepartmentId" : "SUBDEPT19", 
                "labServiceName" : "hello", 
                }
        ]
    }, 
    {
        "subDepartmentId" : "SUBDEPT21", 
        "subDepartmentName" : "Haemotology",
        "labServiceList" : [
            {
                "_id" : "456abc", 
                "subDepartmentId" : "SUBDEPT21", 
                "labServiceName" : "abcd", 
                }
        ]
    }
]

}

从上面的文档中,我只想通过使用其 _id 值检索 labServiceList 的一个对象(例如:“_id”:本文档中的“123abc”)。除了匹配的嵌套文档之外,我不想获得任何其他字段。我试过以下查询:

db.labServiceMasters.aggregate([
{"$project": {
    "subDepartmentList": {"$filter": {
        "input": '$subDepartmentList.labServiceList',
        "as": 'labServiceList',
        "cond": {"$eq": ['$$labServiceList._id', '123abc']}
    }},
    "_id": 0
}}

])

我也尝试过使用 $map 运算符,但没有任何问题。请帮我解决这个问题。也请帮助我在 Java 中使用 mongoTemplate 编写相同的查询。任何建议将不胜感激。提前致谢:-)

您实际上需要在 $filter 中嵌套一个 $map,在 $map 中嵌套另一个 $filter。并使用 $arrayElemAt 获取单个条目:

db.labServiceMasters.aggregate([
  { "$project": {
    "subDepartmentList": {
      "$arrayElemAt": [
        { "$filter": {
          "input": {
            "$map": {
              "input": "$subDepartmentList",
              "as": "sd",
              "in": {
                "$arrayElemAt": [
                  { "$filter": {
                    "input": "$$sd.labServiceList",
                    "as": "ls",
                    "cond": { "$eq": [ "$$ls._id", "123abc" ] }
                  }},
                  0
                ]  
              }
            }
          },
          "as": "sd",
          "cond": { "$ne": [ "$$sd", null ] }
        }},
        0
      ]
    }  
  }}
])

Returns:

{
    "_id" : "PT5",
    "subDepartmentList" : {
        "_id" : "123abc",
        "subDepartmentId" : "SUBDEPT19",
        "labServiceName" : "serviceOne"
    }
}

spring-mongodb 是:

    Aggregation aggregation = newAggregation(
      project("subDepartmentList").and(new AggregationExpression() {
          @Override
          public DBObject toDbObject(AggregationOperationContext context) {
              return new BasicDBObject(
                "$arrayElemAt", Arrays.asList(
                  new BasicDBObject("$filter",
                    new BasicDBObject("input",
                      new BasicDBObject("$map",
                        new BasicDBObject("input","$subDepartmentList")
                          .append("as","sd")
                          .append("in",new BasicDBObject(
                            "$arrayElemAt", Arrays.asList(
                              new BasicDBObject("$filter",
                                new BasicDBObject("input","$$sd.labServiceList")
                                  .append("as","ls")
                                  .append("cond", new BasicDBObject("$eq", Arrays.asList("$$ls._id","123abc")))
                              ),
                              0
                            )
                          ))
                      )
                    )
                    .append("as","sd")
                    .append("$ne", Arrays.asList("$$sd", null))
                  ),
                  0
                )
              );
          }
      }).as("subDepartmentList")
    );

并序列化相同的内容:

{
  "aggregate": "labServiceMasters",
  "pipeline": [
    {
      "$project": {
        "subDepartmentList": {
          "$arrayElemAt": [
            {
              "$filter": {
                "input": {
                  "$map": {
                    "input": "$subDepartmentList",
                    "as": "sd",
                    "in": {
                      "$arrayElemAt": [
                        {
                          "$filter": {
                            "input": "$$sd.labServiceList",
                            "as": "ls",
                            "cond": {
                              "$eq": [
                                "$$ls._id",
                                "123abc"
                              ]
                            }
                          }
                        },
                        0.0
                      ]
                    }
                  }
                },
                "as": "sd",
                "$ne": [
                  "$$sd",
                  null
                ]
              }
            },
            0.0
          ]
        }
      }
    }
  ]
}