SubtractExpression.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. /**
  3. * A $subtract pipeline expression.
  4. * @see evaluate
  5. * @class SubtractExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SubtractExpression = module.exports = function SubtractExpression(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. base.call(this);
  13. }, klass = SubtractExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. var Value = require("../Value");
  16. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName(){
  18. return "$subtract";
  19. };
  20. proto.addOperand = function addOperand(expr) {
  21. this.checkArgLimit(2);
  22. base.prototype.addOperand.call(this, expr);
  23. };
  24. /**
  25. * Takes an array that contains a pair of numbers and subtracts the second from the first, returning their difference.
  26. **/
  27. proto.evaluate = function evaluate(doc) {
  28. this.checkArgCount(2);
  29. var left = this.operands[0].evaluate(doc),
  30. right = this.operands[1].evaluate(doc);
  31. if (left instanceof Date || right instanceof Date) throw new Error("$subtract does not support dates; code 16376");
  32. return left - right;
  33. };