gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:34:21 25 4
gpt4 key购买 nike

我在检索另一个嵌套列表的嵌套文档对象时遇到困难。请帮我解决同样的问题。我的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",
}
]
}
]

}

从上述文档中,我只想使用 labServiceList 的 _id 值检索一个对象(例如:本文档中的“_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
]
}
}}
])

返回:

{
"_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
]
}
}
}
]
}

关于java - 如何获取文档的另一个嵌套列表的嵌套记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45189139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com