SetIntersectionExpression.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. /**
  3. * A $setintersection pipeline expression.
  4. * @see evaluateInternal
  5. * @class SetIntersectionExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetIntersectionExpression = module.exports = function SetIntersectionExpression() {
  11. this.nargs = 2;
  12. base.call(this);
  13. }, klass = SetIntersectionExpression,
  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 "$setIntersection";
  26. };
  27. /**
  28. * Takes 2 objects. Returns the intersects of the objects.
  29. * @method evaluateInternal
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(vars) {
  32. var object1 = this.operands[0].evaluateInternal(vars),
  33. object2 = this.operands[1].evaluateInternal(vars);
  34. if (object1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an object");
  35. if (object2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an object");
  36. var result = object1.filter(function(n) {
  37. return object2.indexOf(n) > -1;
  38. });
  39. };
  40. /** Register Expression */
  41. Expression.registerExpression("$setIntersection", base.parse(SetIntersectionExpression));