SizeExpression.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. /**
  3. * A $size pipeline expression.
  4. * @see evaluateInternal
  5. * @class SizeExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SizeExpression = module.exports = function SizeExpression() {
  11. this.fixedArity(1);
  12. if (arguments.length !== 1) throw new Error("one arg expected");
  13. base.call(this);
  14. }, klass = SizeExpression,
  15. base = require("./NaryExpression"),
  16. proto = klass.prototype = Object.create(base.prototype, {
  17. constructor: {
  18. value: klass
  19. }
  20. });
  21. // DEPENDENCIES
  22. var Value = require("../Value"),
  23. Expression = require("./Expression");
  24. // PROTOTYPE MEMBERS
  25. proto.getOpName = function getOpName() {
  26. return "$size";
  27. };
  28. /**
  29. * Takes an array and return the size.
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(doc) {
  32. this.checkArgCount(1);
  33. var array = this.operands[0].evaluateInternal(doc);
  34. if (array instanceof Date) throw new Error("$size does not support dates; code 16376");
  35. return array.length;
  36. };
  37. /** Register Expression */
  38. Expression.registerExpression("$size", SizeExpression.parse);