AddToSetAccumulator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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() {
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. this.reset();
  12. base.call(this);
  13. }, klass = AddToSetAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. var ValueSet = require("../ValueSet");
  15. proto.processInternal = function processInternal(input, merging) {
  16. if (!merging) {
  17. if (input !== undefined) {
  18. this.set.insert(input);
  19. }
  20. } else {
  21. // If we're merging, we need to take apart the arrays we
  22. // receive and put their elements into the array we are collecting.
  23. // If we didn't, then we'd get an array of arrays, with one array
  24. // from each merge source.
  25. if (!Array.isArray(input)) throw new Error("Assertion failure");
  26. for (var i = 0, l = input.length; i < l; i++) {
  27. this.set.insert(input[i]);
  28. }
  29. }
  30. };
  31. proto.getValue = function getValue(toBeMerged) { //jshint ignore:line
  32. return this.set.values();
  33. };
  34. proto.reset = function reset() {
  35. this.set = new ValueSet();
  36. };
  37. klass.create = function create() {
  38. return new AddToSetAccumulator();
  39. };
  40. proto.getOpName = function getOpName() {
  41. return "$addToSet";
  42. };