SetUnionExpression.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. /**
  3. * A $setunion pipeline expression.
  4. * @see evaluateInternal
  5. * @class SetUnionExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetUnionExpression = module.exports = function SetUnionExpression() {
  11. base.call(this);
  12. }, klass = SetUnionExpression, base = require("./NaryBaseExpressionT")(SetUnionExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // DEPENDENCIES
  14. var Value = require("../Value"),
  15. Expression = require("./Expression");
  16. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName() {
  18. return "$setUnion";
  19. };
  20. /**
  21. * Takes 2 objects. Returns the union of the two objects.
  22. * @method evaluateInternal
  23. **/
  24. proto.evaluateInternal = function evaluateInternal(vars) {
  25. var unionSet = {};
  26. var object1 = this.operands[0].evaluateInternal(vars),
  27. object2 = this.operands[1].evaluateInternal(vars);
  28. //Deviation from Mongo. We are using objects for this, while they use arrays.
  29. if (typeof object1 != object) throw new Error("All operands of " + this.getOpName() + "must be objects. First argument is of type: " + typeof object1);
  30. if (typeof object2 != object) throw new Error("All operands of " + this.getOpName() + "must be objects. Second argument is of type: " + typeof object2);
  31. for (var attrname1 in object1) {
  32. unionSet[attrname1] = object1[attrname1];
  33. }
  34. for (var attrname2 in object2) {
  35. unionSet[attrname2] = object2[attrname2];
  36. }
  37. return unionSet;
  38. };
  39. /** Register Expression */
  40. Expression.registerExpression("$setUnion", base.parse);