Skip to content

Arithmetic Operators

ArithmeticOperators đại diện cho nhóm toán tử số học của MongoDB. Nó cung cấp các static method và builder để tạo biểu thức từ AggregationExpression, field hoặc literal, trả về instance toán tử tương ứng mà không cần tự viết BSON thủ công.

import { ArithmeticOperators } from 'red-aggregation/operator/arithmeticOperators/arithmeticOperators';

Phương thức

ArithmeticOperators.abs()

ArithmeticOperators.abs('price').toDocument(context);
// => { $abs: '$price' }

Hoặc:

ArithmeticOperators.abs(-10).toDocument(context);
// => { $abs: -10 }

ArithmeticOperators.add()

ArithmeticOperators.add('price')
  .add(5)
  .add('discount')
  .toDocument(context);
// => { $add: ['$price', 5, '$discount'] }

ArithmeticOperators.divide()

ArithmeticOperators.divide('total')
  .divideBy(10)
  .divideBy('count')
  .toDocument(context);
// => { $divide: ['$total', 10, '$count'] }

ArithmeticOperators.ceil() / floor()

ArithmeticOperators.ceil({ $divide: ['$total', '$count'] }).toDocument(context);
// => { $ceil: { $divide: ['$total', '$count'] } }

ArithmeticOperators.floor('price').toDocument(context);
// => { $floor: '$price' }

ArithmeticOperators.exp()

ArithmeticOperators.exp({ $add: [1, 2] }).toDocument(context);
// => { $exp: { $add: [1, 2] } }

ArithmeticOperators.log() / log10() / ln()

ArithmeticOperators.log('value').log(2);
// => { $log: ['$value', 2] }

ArithmeticOperators.log10(100);
// => { $log10: 100 }

ArithmeticOperators.ln('$value');
// => { $ln: '$value' }

ArithmeticOperators.mod()

ArithmeticOperators.mod('amount').mod(3);
// => { $mod: ['$amount', 3] }

ArithmeticOperators.multiply()

ArithmeticOperators.multiply('price')
  .multiplyBy(2)
  .multiplyBy('$tax')
  .toDocument(context);
// => { $multiply: ['$price', 2, '$tax'] }

ArithmeticOperators.pow()

ArithmeticOperators.pow('$value').pow(3);
// => { $pow: ['$value', 3] }

ArithmeticOperators.round()

ArithmeticOperators.round('price').place(2).toDocument(context);
// => { $round: ['$price', 2] }

ArithmeticOperators.sqrt()

ArithmeticOperators.sqrt('$variance');
// => { $sqrt: '$variance' }

ArithmeticOperators.subtract()

ArithmeticOperators.subtract('total')
  .subtract('$discount')
  .subtract(5)
  .toDocument(context);
// => { $subtract: ['$total', '$discount', 5] }

ArithmeticOperators.trunc()

ArithmeticOperators.trunc('$value');
// => { $trunc: '$value' }

Lưu ý sử dụng

  • Các builder hỗ trợ chain nhiều toán hạng theo thứ tự gọi (.add(), .divideBy(), .multiplyBy(), .subtract()…).
  • round().place(n) chỉ định số chữ số thập phân; nếu không gọi, MongoDB mặc định 0.
  • Khi cần xây dựng nhiều phép toán dựa trên cùng một input, nên tái sử dụng cùng một builder được khởi tạo từ ArithmeticOperators.*(...) thay vì lặp lại chuẩn hóa đầu vào.