AddToSetAccumulator.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. /**
  3. * Create an expression that finds the sum of n operands.
  4. * @class AddSoSetAccumulator
  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. proto.processInternal = function processInternal(doc, merging) {
  17. var set = this.set;
  18. Object.keys(doc).map(function(d) {
  19. set[JSON.stringify(doc[d])] = doc[d];
  20. });
  21. };
  22. proto.getValue = function getValue(toBeMerged) {
  23. var set = this.set;
  24. return Object.keys(set).map(function(k) {
  25. return set[k];
  26. });
  27. };
  28. proto.reset = function reset() {
  29. this.set = {};
  30. };
  31. // PROTOTYPE MEMBERS
  32. proto.getOpName = function getOpName(){
  33. return "$addToSet";
  34. };