【发布时间】:2023-03-28 10:05:01
【问题描述】:
我有以下文件
[ {
"pageName": "Content_2",
"domain": "bingo.com",
"locale": "en-us",
"contents": [
{
"contentName": "Template_2",
"fields": [
{
"title": "Company Name"
},
{
"title": "Designation"
}
]
}
]
},
{
"version": 2,
"pageName": "Content_3",
"domain": "bingo.com",
"locale": "en-in",
"contents": [
{
"contentName": "Template_2",
"fields": [
{
"title": "Company Name"
},
{
"title": "Designation"
}
]
}
]
}]
我正在根据域、区域设置和内容名称过滤数据。到目前为止,一切正常。现在,在 fields 数组中,我只想显示那些标题与特定值匹配的字段。我无法弄清楚如何通过聚合操作来做到这一点。我在sn-p下面试过了:
Aggregation aggregation = newAggregation(
match(
where("domain").is(domain)
.and("contents.contentName").is(templateName)
.and("locale").in(criteria.getLocales())),
project().and(new AggregationExpression() {
@Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
DBObject filterExpression = new BasicDBObject();
filterExpression.put("input", "$contents.fields");
filterExpression.put("as", "field");
filterExpression.put("cond",
new BasicDBObject("$eq", Arrays.<Object>asList("$$field.title", "Company Name")));
return new Document("$filter", filterExpression);
}
}).as("field"));
AggregationResults<MyClass> list = mongoOperations.aggregate(aggregation, MyClass.class,
MyClass.class);
它将所有属性都返回为空。请指导。我是 MongoDB 的新手。提前致谢;
【问题讨论】:
标签: mongodb spring-boot spring-data aggregation-framework mongotemplate