SetUnionExpression.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. /**
  3. * A $setunion pipeline expression.
  4. * @class SetUnionExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. var SetUnionExpression = module.exports = function SetUnionExpression() {
  10. if (arguments.length !== 0) throw new Error(klass.name + ": no args expected");
  11. base.call(this);
  12. }, klass = SetUnionExpression, base = require("./VariadicExpressionT")(SetUnionExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. var Value = require("../Value"),
  14. Expression = require("./Expression"),
  15. ValueSet = require("../ValueSet");
  16. proto.evaluateInternal = function evaluateInternal(vars) {
  17. var unionedSet = new ValueSet(),
  18. n = this.operands.length;
  19. for (var i = 0; i < n; i++){
  20. var newEntries = this.operands[i].evaluateInternal(vars);
  21. if (newEntries === undefined || newEntries === null){
  22. return null;
  23. }
  24. if (!(newEntries instanceof Array))
  25. throw new Error("All operands of " + this.getOpName() + "must be arrays. One argument" +
  26. " is of type: " + Value.getType(newEntries) + "; uassert code 17043");
  27. unionedSet.insertRange(newEntries);
  28. }
  29. return unionedSet.values();
  30. };
  31. Expression.registerExpression("$setUnion", base.parse);
  32. proto.getOpName = function getOpName() {
  33. return "$setUnion";
  34. };
  35. proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() {
  36. return true;
  37. };