AddToSetAccumulator.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName(){
  18. return "$addToSet";
  19. };
  20. proto.getFactory = function getFactory(){
  21. return klass; // using the ctor rather than a separate .create() method
  22. };
  23. proto.evaluate = function evaluate(doc) {
  24. if (arguments.length !== 1) throw new Error("One and only one arg expected");
  25. var rhs = this.operands[0].evaluate(doc);
  26. if (rhs === undefined) return;
  27. this.set[JSON.stringify(rhs)] = rhs;
  28. };
  29. proto.getValue = function getValue() {
  30. var setValues = [];
  31. for (var setKey in this.set) {
  32. setValues.push(this.set[setKey]);
  33. }
  34. return setValues;
  35. };