AddToSetAccumulator.js 1.4 KB

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