AddToSetAccumulator.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. /**
  3. * Create an expression that finds the sum of n operands.
  4. * @class AddToSetAccumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var AddToSetAccumulator = module.exports = function AddToSetAccumulator(/* ctx */){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. this.set = [];
  12. //this.itr = undefined; /* Shoudln't need an iterator for the set */
  13. //this.ctx = undefined; /* Not using the context object currently as it is related to sharding */
  14. base.call(this);
  15. }, klass = AddToSetAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  16. // NOTE: Skipping the create function, using the constructor instead
  17. // DEPENDENCIES
  18. var Value = require("../Value");
  19. // MEMBER FUNCTIONS
  20. proto.getOpName = function getOpName(){
  21. return "$addToSet";
  22. };
  23. proto.contains = function contains(value) {
  24. var set = this.set;
  25. for (var i = 0, l = set.length; i < l; ++i) {
  26. if (Value.compare(set[i], value) === 0) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. };
  32. proto.processInternal = function processInternal(input, merging) {
  33. if (! this.contains(input)) {
  34. this.set.push(input);
  35. }
  36. };
  37. proto.getValue = function getValue(toBeMerged) {
  38. return this.set;
  39. };
  40. proto.reset = function reset() {
  41. this.set = [];
  42. };