AddToSetAccumulator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. var AddToSetAccumulator = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * Create an expression that finds the sum of n operands.
  6. * @class AddSoSetAccumulator
  7. * @namespace munge.pipeline.accumulators
  8. * @module munge
  9. * @constructor
  10. **/
  11. var klass = module.exports = function AddToSetAccumulator(/* pCtx */){
  12. if(arguments.length !== 0) throw new Error("zero args expected");
  13. this.set = {};
  14. //this.itr = undefined; /* Shoudln't need an iterator for the set */
  15. //this.pCtx = undefined; /* Not using the context object currently as it is related to sharding */
  16. base.call(this);
  17. }, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. // PROTOTYPE MEMBERS
  19. proto.getOpName = function getOpName(){
  20. return "$addToSet";
  21. };
  22. proto.getFactory = function getFactory(){
  23. return klass; // using the ctor rather than a separate .create() method
  24. };
  25. proto.evaluate = function evaluate(doc) {
  26. if(arguments.length !== 1) throw new Error("One and only one arg expected");
  27. var rhs = this.operands[0].evaluate(doc);
  28. if (rhs === undefined) return;
  29. this.set[JSON.stringify(rhs)] = rhs;
  30. };
  31. proto.getValue = function getValue() {
  32. var setValues = [];
  33. for (var setKey in this.set) {
  34. setValues.push(this.set[setKey]);
  35. }
  36. return setValues;
  37. };
  38. return klass;
  39. })();