Accumulator Operators¶
AccumulatorOperators đại diện cho nhóm toán tử tích lũy của MongoDB: $sum, $avg, $min, $max và biến thể $minN/$maxN. Từ field, AggregationExpression hoặc literal (với $sum), các builder giúp bạn tạo biểu thức mà không phải viết BSON thủ công.
import { AccumulatorOperators } from 'red-aggregation/operator/accumulatorOperators/accumulatorOperators';
Phương thức¶
AccumulatorOperators.valueOf()¶
Khởi tạo factory từ field hoặc AggregationExpression, sau đó chain tới toán tử mong muốn.
AccumulatorOperators.valueOf('price').sum().and('$tax').toDocument(context);
// => { $sum: ['$price', '$tax'] }
AccumulatorOperators.valueOf({ $subtract: ['$x', '$y'] }).avg().and('$z').toDocument(context);
// => { $avg: [{ $subtract: ['$x', '$y'] }, '$z'] }
Sum.sumOf()¶
Sum.sumOf('price').and('$tax').toDocument(context);
// => { $sum: ['$price', '$tax'] }
Hoặc:
Sum.sumOf('subtotal')
.and('$discount')
.and({ $multiply: ['$fee', 2] })
.and(5)
.toDocument(context);
// => { $sum: ['$subtotal', '$discount', { $multiply: ['$fee', 2] }, 5] }
Avg.avgOf()¶
Avg.avgOf('score').and('$bonus').toDocument(context);
// => { $avg: ['$score', '$bonus'] }
Hoặc:
Avg.avgOf('total')
.and({ $multiply: ['$price', '$quantity'] })
.toDocument(context);
// => { $avg: ['$total', { $multiply: ['$price', '$quantity'] }] }
Min.minOf()¶
Min.minOf('price').toDocument(context);
// => { $min: '$price' }
Hoặc:
Min.minOf('price').and('discount').toDocument(context);
// => { $min: ['$price', '$discount'] }
Giới hạn N phần tử nhỏ nhất:
Min.minOf('score').limit(3).toDocument(context);
// => { $minN: { input: '$score', n: 3 } }
Min.minOf('score').and('bonus').limit(5).toDocument(context);
// => { $minN: { input: ['$score', '$bonus'], n: 5 } }
Max.maxOf()¶
Max.maxOf('price').toDocument(context);
// => { $max: '$price' }
Hoặc:
Max.maxOf('price').and('discount').toDocument(context);
// => { $max: ['$price', '$discount'] }
Giới hạn N phần tử lớn nhất:
Max.maxOf('score').limit(3).toDocument(context);
// => { $maxN: { input: '$score', n: 3 } }
Max.maxOf('score').and('bonus').limit(5).toDocument(context);
// => { $maxN: { input: ['$score', '$bonus'], n: 5 } }
Lưu ý sử dụng¶
- Factory chỉ nhận field reference hoặc
AggregationExpression. Giá trị không hợp lệ sẽ bị từ chối. Sum.and(...)cho phép literal số;Avg/Min/Maxchấp nhận field hoặc expression.- Gọi
limit(n)trênMin/Maxsẽ chuyển sang$minN/$maxN. Nếu chỉ cần một giá trị nhỏ/lớn nhất, không gọilimit.