英文:
How can I filter sub-documents from MongoDB with SpringBoot?
问题
以下是翻译好的部分:
我在一个集合中有这样的文档:
{
"login": "xxx",
"someAttribute": "someValue",
"subDocs": [
{
"data": "val1",
"subDocNestedArray": ["WWW", "AAAA"]
},
{
"data": "val2",
"subDocNestedArray": ["AAAA"]
},
{
"data": "val3",
"subDocNestedArray": ["ZZZ"]
}
]
}
我检索所有匹配 login="xxx" 且 subDocNestedArray 包含 "AAAA" 的文档,但只希望返回与根文档的其他属性匹配的子文档,例如:
{
"login": "xxx",
"someAttribute": "someValue",
"subDocs": [
{
"data": "val1",
"subDocNestedArray": ["WWW", "AAAA"]
},
{
"data": "val2",
"subDocNestedArray": ["AAAA"]
}
]
}
我尝试过以下方式:
Aggregation.unwind("$subDocs"),
Aggregation.match(Criteria.where("subDocs.subDocNestedArray").is("AAAA")),
Aggregation.group("$_id")
但结果中缺少字段:
{
"login": null,
"someAttribute": null,
"subDocs": [
{
"data": "val1",
"subDocNestedArray": ["WWW", "AAAA"]
},
{
"data": "val2",
"subDocNestedArray": ["AAAA"]
}
]
}
我没有成功找到使用 "redact" 的语法。有什么线索吗?
使我的 MongoDB 查询返回经过过滤的结果。
英文:
I have documents in a collection such as :
{
"login":"xxx",
"someAttribute":"someValue",
"subDocs":[
{
"data": "val1",
"subDocNestedArray":["WWW","AAAA"]
},
{
"data": "val2",
"subDocNestedArray":["AAAA"]
},
{
"data": "val3",
"subDocNestedArray":["ZZZ"]
}
]
}
I retrieve all documents that match login="xxx" and subDocNestedArray="AAAA" but want only subDocs matching along other properties of the root document, ex :
{
"login":"xxx",
"someAttribute":"someValue",
"subDocs":[
{
"data": "val1",
"subDocNestedArray":["WWW","AAAA"]
},
{
"data": "val2",
"subDocNestedArray":["AAAA"]
}
]
}
I have tried with :
Aggregation.unwind("$subDocs"),
Aggregation.match(Criteria.where("subDocs.subDocNestedArray").is("AAAA")),
Aggregation.group("$_id")
that results in missing fields
{
"login": null,
"someAttribute": null,
"subDocs":[
{
"data": "val1",
"subDocNestedArray":["WWW","AAAA"]
},
{
"data": "val2",
"subDocNestedArray":["AAAA"]
}
]
}
I did not manage to find the syntax using "redact".
Any clue ?
Make my MongoDB query return filtered results
答案1
得分: 1
你可以尝试这样做:
MatchOperation match = match(Criteria.where("login").is("xxx").and("subDocs.subDocNestedArray").is("AAAA"));
AddFieldsOperation addField = AddFieldsOperation.addField("subDocs").withValueOf(ArrayOperators.Filter.filter("subDocs").as("item").by(ArrayOperators.In.arrayOf("$$item.subDocNestedArray").containsValue("AAAA")));
Aggregation agg = Aggregation.newAggregation(match, addField);
AggregationResults<XYZClass> aggResults = mongoTemplate.aggregate(agg, "CollectionName", XYZClass.class);
在这个代码中,我们有一个$match
阶段来过滤文档,然后是一个addFields
阶段,用于过滤subDocs
数组。请尝试一下。
英文:
You can try this:
MatchOperation match = match(Criteria.where("login").is("xxx").and("subDocs.subDocNestedArray").is("AAAA"));
AddFieldsOperation addField = AddFieldsOperation.addField("subDocs").withValueOf(ArrayOperators.Filter.filter("subDocs").as("item").by(ArrayOperators.In.arrayOf("$$item.subDocNestedArray").containsValue("AAAA")));
Aggregation agg = Aggregation.newAggregation(match, addField);
AggregationResults<XYZClass> aggResults = mongoTemplate.aggregate(agg, "CollectionName",XYZClass.class);
In this, we have a $match
stage to filter documents, and then an addFields
stage, to filter the subDocs
array. Please test it out.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论