MonthExpression.js 1017 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. var MonthExpression = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A $month pipeline expression.
  6. *
  7. * @see evaluate
  8. * @class MonthExpression
  9. * @namespace mungedb.aggregate.pipeline.expressions
  10. * @module mungedb-aggregate
  11. * @constructor
  12. **/
  13. var klass = function MonthExpression(){
  14. if(arguments.length !== 0) throw new Error("zero args expected");
  15. base.call(this);
  16. }, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  17. // PROTOTYPE MEMBERS
  18. proto.getOpName = function getOpName(){
  19. return "$month";
  20. };
  21. proto.addOperand = function addOperand(expr) {
  22. this.checkArgLimit(1);
  23. base.prototype.addOperand.call(this, expr);
  24. };
  25. /**
  26. * Takes a date and returns the month as a number between 1 and 12.
  27. * @method evaluate
  28. **/
  29. proto.evaluate = function evaluate(doc){
  30. this.checkArgCount(1);
  31. var date = this.operands[0].evaluate(doc);
  32. return date.getMonth() + 1;
  33. };
  34. return klass;
  35. })();