AddToSetAccumulator.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.getFactory = function getFactory(){
  24. return klass; // using the ctor rather than a separate .create() method
  25. };
  26. proto.contains = function contains(value) {
  27. var set = this.set;
  28. for (var i = 0, l = set.length; i < l; ++i) {
  29. if (Value.compare(set[i], value) === 0) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. };
  35. proto.processInternal = function processInternal(input, merging) {
  36. if (! this.contains(input)) {
  37. this.set.push(input);
  38. }
  39. };
  40. proto.getValue = function getValue(toBeMerged) {
  41. return this.set;
  42. };
  43. proto.reset = function reset() {
  44. this.set = [];
  45. };