MonthExpression.js 950 B

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