img

3 聚合管道操作

下面是基于聚合管道mongodb提供的可操作的内容

支持的操作java接口说明
$projectAggregation.project修改输入文档的结构。
$matchAggregation.match用于过滤数据
$limitAggregation.limit用来限制MongoDB聚合管道返回的文档数
$skipAggregation.skip在聚合管道中跳过指定数量的文档
$unwindAggregation.unwind将文档中的某一个数组类型字段拆分成多条
$groupAggregation.group将集合中的文档分组,可用于统计结果
$sortAggregation.sort将输入文档排序后输出
$geoNearAggregation.geoNear输出接近某一地理位置的有序文档
$lookupAggregation.lookup多表联合

4 聚合内容操作

下面是基于聚合操作(Aggregation.group),mongodb提供可选的表达式

聚合表达式java接口说明
$sumAggregation.group().sum(“field”).as(“sum”)求和
$avgAggregation.group().avg(“field”).as(“avg”)求平均
$minAggregation.group().min(“field”).as(“min”)最小值
$maxAggregation.group().max(“field”).as(“max”)最大值
$pushAggregation.group().push(“field”).as(“push”)在结果文档中插入值到一个数组中
$addToSetAggregation.group().addToSet(“field”).as(“addToSet”)在结果文档中插入值到一个数组中,但不创建副本
$firstAggregation.group().first(“field”).as(“first”)根据资源文档的排序获取第一个文档数据
$lastAggregation.group().last(“field”).as(“last”)根据资源文档的排序获取最后一个文档数据

官方文档:https://www.mongodb.com/docs/mongodb-shell/ 最好的学习资料