YearExpression.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. /**
  3. * A $year pipeline expression.
  4. * @see evaluate
  5. * @class YearExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var YearExpression = module.exports = function YearExpression(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. base.call(this);
  13. }, klass = YearExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // DEPENDENCIES
  15. var Value = require("../Value"),
  16. DayOfYearExpression = require("./DayOfYearExpression");
  17. // PROTOTYPE MEMBERS
  18. proto.getOpName = function getOpName(){
  19. return "$year";
  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 full year.
  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.getUTCFullYear();
  33. };