MongoDB 聚合框架提供了以下类型的聚合操作:
管道聚合操作符
组/累加器聚合操作符
布尔聚合操作符
比较聚合操作符
算术聚合操作符
字符串聚合操作符
日期聚合操作符
数组聚合操作符
条件聚合操作符
查询聚合操作符
转换聚合操作符
对象聚合操作符
脚本聚合操作符
而 Spring Data MongoDB 库所支持的操作符如下:
管道聚合操作符:bucket, bucketAuto, count, facet, geoNear, graphLookup, group, limit, lookup, match, project, rand, replaceRoot, skip, sort, unwind
集合聚合操作符:setEquals, setIntersection, setUnion, setDifference, setIsSubset, anyElementTrue, allElementsTrue
组/累加器聚合运算符:addToSet, bottom, bottomN, covariancePop, covarianceSamp, expMovingAvg, first, firstN, last, lastN max, maxN, min, minN, avg, push, sum, top, topN, count (*), stdDevPop, stdDevSamp
布尔聚合运算符:
比较聚合运算符:eq (* via is), gt, gte, lt, lte, ne
算术聚合运算符:abs, acos, acosh, add (* via plus), asin, asin, atan, atan2, atanh, ceil, cos, cosh, derivative, divide, exp, floor, integral, ln, log, log10, mod, multiply, pow, round, sqrt, subtract (* via minus), sin, sinh, tan, tanh, trunc
字符串聚合运算符:concat, substr, toLower, toUpper, strcasecmp, indexOfBytes, indexOfCP, regexFind, regexFindAll, regexMatch, replaceAll, replaceOne, split`, strLenBytes, strLenCP, substrCP, trim, ltrim, rtim
日期聚合运算符:dateSubstract, dateTrunc, dayOfYear, dayOfMonth, dayOfWeek, year, month, week, hour, minute, second, millisecond, dateAdd, dateDiff, dateToString, dateFromString, dateFromParts, dateToParts, isoDayOfWeek, isoWeek, isoWeekYear, tsIncrement, tsSecond
数组聚合运算符:arrayElementAt, arrayToObject, concatArrays, filter, first, in, indexOfArray, isArray, last, range`, reverseArray, reduce, size, sortArray, slice, zip
迭代运算符:literal
变量操作符:map
条件聚合运算符:cond, ifNull, switch
类型聚合运算符:type
查询聚合运算符:
转换聚合运算符:convert, degreesToRadians, toBool, toDate, toDecimal, toDouble, toInt, toLong, toObjectId, toString
对象聚合运算符:objectToArray, mergeObjects, getField, setField
脚本聚合运算符:function, accumulator
注意,上面没有列出的聚合操作目前不被 Spring Data MongoDB 所支持(参考 https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation.supported-aggregation-operations),比较聚合运算符以 Criteria 表达式的形式表示。
提示:不支持的聚合操作/操作符(如 MongoDB Atlas 的 $search)可以通过实现 AggregationOperation 或 AggregationExpression 来提供。Aggregation.stage 是通过提供 JSON 或 Bson 表示来注册管道阶段的快捷方式。如下:
Aggregation.stage(""" { $search : { "near": { "path": "released", "origin": { "$date": { "$numberLong": "..." } } , "pivot": 7 } } } """);