SetEqualsExpression.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. /**
  3. * A $setequals pipeline expression.
  4. * @class SetEqualsExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. */
  9. var SetEqualsExpression = module.exports = function SetEqualsExpression() {
  10. if (arguments.length !== 0) throw new Error(klass.name + ": no args expected");
  11. base.call(this);
  12. }, klass = SetEqualsExpression, base = require("./VariadicExpressionT")(SetEqualsExpression), 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.validateArguments = function validateArguments(args) {
  17. if (args.length < 2)
  18. throw new Error(this.getOpName() + " needs at least two arguments had: " +
  19. args.length + "; uassert code 17045");
  20. };
  21. proto.evaluateInternal = function evaluateInternal(vars) {
  22. var n = this.operands.length,
  23. lhs;
  24. for (var i = 0; i < n; i++) {
  25. var nextEntry = this.operands[i].evaluateInternal(vars);
  26. if (!(nextEntry instanceof Array))
  27. throw new Error("All operands of " + this.getOpName() +" must be arrays. One " +
  28. "argument is of type: " + Value.getType(nextEntry) + "; uassert code 17044");
  29. if (i === 0) {
  30. lhs = new ValueSet(nextEntry);
  31. } else {
  32. var rhs = new ValueSet(nextEntry);
  33. if (!lhs.equals(rhs)) {
  34. return false;
  35. }
  36. }
  37. }
  38. return true;
  39. };
  40. Expression.registerExpression("$setEquals", base.parse);
  41. proto.getOpName = function getOpName() {
  42. return "$setEquals";
  43. };