
3 聚合管道操作
下面是基于聚合管道mongodb提供的可操作的内容
| 支持的操作 | java接口 | 说明 |
|---|---|---|
| $project | Aggregation.project | 修改输入文档的结构。 |
| $match | Aggregation.match | 用于过滤数据 |
| $limit | Aggregation.limit | 用来限制MongoDB聚合管道返回的文档数 |
| $skip | Aggregation.skip | 在聚合管道中跳过指定数量的文档 |
| $unwind | Aggregation.unwind | 将文档中的某一个数组类型字段拆分成多条 |
| $group | Aggregation.group | 将集合中的文档分组,可用于统计结果 |
| $sort | Aggregation.sort | 将输入文档排序后输出 |
| $geoNear | Aggregation.geoNear | 输出接近某一地理位置的有序文档 |
| $lookup | Aggregation.lookup | 多表联合 |
4 聚合内容操作
下面是基于聚合操作(Aggregation.group),mongodb提供可选的表达式
| 聚合表达式 | java接口 | 说明 |
|---|---|---|
| $sum | Aggregation.group().sum(“field”).as(“sum”) | 求和 |
| $avg | Aggregation.group().avg(“field”).as(“avg”) | 求平均 |
| $min | Aggregation.group().min(“field”).as(“min”) | 最小值 |
| $max | Aggregation.group().max(“field”).as(“max”) | 最大值 |
| $push | Aggregation.group().push(“field”).as(“push”) | 在结果文档中插入值到一个数组中 |
| $addToSet | Aggregation.group().addToSet(“field”).as(“addToSet”) | 在结果文档中插入值到一个数组中,但不创建副本 |
| $first | Aggregation.group().first(“field”).as(“first”) | 根据资源文档的排序获取第一个文档数据 |
| $last | Aggregation.group().last(“field”).as(“last”) | 根据资源文档的排序获取最后一个文档数据 |
官方文档:https://www.mongodb.com/docs/mongodb-shell/ 最好的学习资料
