SubtractExpression.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.fixedAirty(2);
  12. if (arguments.length !== 0) throw new Error("zero args expected");
  13. base.call(this);
  14. }, klass = SubtractExpression,
  15. base = require("./NaryExpression"),
  16. proto = klass.prototype = Object.create(base.prototype, {
  17. constructor: {
  18. value: klass
  19. }
  20. });
  21. // DEPENDENCIES
  22. var Value = require("../Value"),
  23. Expression = require("./Expression");
  24. // PROTOTYPE MEMBERS
  25. proto.getOpName = function getOpName() {
  26. return "$subtract";
  27. };
  28. /**
  29. * Takes an array that contains a pair of numbers and subtracts the second from the first, returning their difference.
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(doc) {
  32. this.checkArgCount(2);
  33. var left = this.operands[0].evaluateInternal(doc),
  34. right = this.operands[1].evaluateInternal(doc);
  35. if (left instanceof Date || right instanceof Date) throw new Error("$subtract does not support dates; code 16376");
  36. return left - right;
  37. };
  38. /** Register Expression */
  39. Expression.registerExpression("$subtract", SubtractExpression.parse);