SetDifferenceExpression.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. /**
  3. * A $setdifference pipeline expression.
  4. * @see evaluateInternal
  5. * @class SetDifferenceExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetDifferenceExpression = module.exports = function SetDifferenceExpression() {
  11. base.call(this);
  12. }, klass = SetDifferenceExpression,
  13. FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
  14. base = FixedArityExpression,
  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 "$setdifference";
  26. };
  27. /**
  28. * Takes 2 arrays. Assigns the second array to the first array.
  29. * @method evaluateInternal
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(vars) {
  32. var array1 = this.operands[0].evaluateInternal(vars),
  33. array2 = this.operands[1].evaluateInternal(vars);
  34. if (array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array");
  35. if (array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array");
  36. var returnVec = [];
  37. array1.forEach(function(key) {
  38. if (-1 === array2.indexOf(key)) {
  39. returnVec.push(key);
  40. }
  41. }, this);
  42. return returnVec;
  43. };
  44. /** Register Expression */
  45. Expression.registerExpression("$setdifference", base.parse);