【发布时间】:2017-10-09 05:37:09
【问题描述】:
我们尝试了 $project (Aggregation) 和 Include Fields(Query),但它只包括 Id 和 Filtered 字段。我们预期的结果是文档中的所有字段以及嵌入文档都按区域设置(动态键)过滤。
例子:
categories:[{
_id: 1,
categoryNumber: "12345",
locale: {
en_US: {
name: "Category Name in US"
},
fr_FR: {
name: "Category Name in French"
}
}
},
{
_id: 2,
categoryNumber: "6789",
locale: {
en_GB: {
name: "Category Name in UK"
}
}
}]
Expected Result:
Filter records by locale: en_US(Embedded document key)
[{
_id: 1,
categoryNumber: "12345",
locale: {
en_US: {
name: "Category Name in US"
}
}
}]
查询:
AggregationOperation matchOperaion = match(where(Constants.ID).is(clientProdTypeId));
AggregationExpression aggregationExpression = (AggregationOperationContext) -> {
Map<String, Object> conditionMap = new HashMap<>();
conditionMap.put("input", "$locale.en_US.name");
conditionMap.put("as", Constants.NAME);
conditionMap.put("cond", new BasicDBObject());
return new BasicDBObject("$filter", conditionMap);
};
AggregationOperation projectionOperation;
projectionOperation = project().and(aggregationExpression).as(Constants.METADATA)
.andInclude(Constants.CLIENT_ID, Constants.PRODUCT_TYPE_ID,Constants.STATUS);
AggregationResults<CategoryDTO> result;
try {
result = mongoOperations.aggregate(newAggregation(matchOperaion, projectionOperation),
Category.class, CategoryDTO.class);
} catch (IllegalArgumentException | DataAccessException e) {
LOGGER.error("Error while fetching client product type", e);
}
return result.getUniqueMappedResult();
【问题讨论】:
-
您的聚合查询在哪里?
-
我已经更新了问题
标签: mongodb mongodb-query spring-data aggregation-framework