SubtractExpression.js 1.2 KB

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