【发布时间】:2021-05-23 05:56:35
【问题描述】:
我从两个集合中进行了 Aggregations.lookup,返回了商店列表,每个都有显示产品数组的字段,每个产品都有它的价格。 任务是接收投影,为每个商店展示:
- 产品总量
- 平均产品价格
- 最贵和最便宜产品的价格
- 以及价格低于10的产品数量
问题是我不太了解如何完成第四个任务,因为我不太了解 projections.computed 的语法。这是代码
AggregateIterable<Document> it = storesCollection.aggregate(Arrays.asList(
lookup("Products", "products", "name", "products_displayed")
, project(fields(
include("name")
, excludeId()
, computed("total", computed("$size", "$products_displayed")) //total
, computed("avgprice", computed("$avg", "$products_displayed.price")) //avg
, computed("maxprice", computed("$max", "$products_displayed.price")) //max
, computed("minprice", computed("$min", "$products_displayed.price")) //min
, computed("total lt 10", computed("$size", )) // this is the problem: count total amount of products with prices less than 10
)
)
)
);
【问题讨论】:
标签: java mongodb aggregation-framework projection