SetUnionExpression.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. this.fixedArity(2);
  12. if (arguments.length !== 2) throw new Error("two args expected");
  13. base.call(this);
  14. }, klass = SetUnionExpression,
  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 "$setunion";
  27. };
  28. /**
  29. * Takes 2 objects. Unions the objects
  30. * @method evaluateInternal
  31. **/
  32. proto.evaluateInternal = function evaluateInternal(doc) {
  33. var object1 = this.operands[0].evaluateInternal(doc),
  34. object2 = this.operands[1].evaluateInternal(doc);
  35. if (object1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an object");
  36. if (object2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an object");
  37. var object3 = {};
  38. for (var attrname1 in object1) {
  39. object3[attrname1] = object1[attrname1];
  40. }
  41. for (var attrname2 in object2) {
  42. object3[attrname2] = object2[attrname2];
  43. }
  44. return object3;
  45. };
  46. /** Register Expression */
  47. Expression.registerExpression("$setunion", SetUnionExpression.parse);