IfNullExpression.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. /**
  3. * An $ifNull pipeline expression.
  4. * @see evaluate
  5. * @class IfNullExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var IfNullExpression = module.exports = function IfNullExpression(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. base.call(this);
  13. }, klass = IfNullExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // PROTOTYPE MEMBERS
  15. proto.getOpName = function getOpName(){
  16. return "$ifNull";
  17. };
  18. proto.addOperand = function addOperand(expr) {
  19. this.checkArgLimit(2);
  20. base.prototype.addOperand.call(this, expr);
  21. };
  22. /**
  23. * Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] }
  24. * @method evaluate
  25. **/
  26. proto.evaluate = function evaluate(doc){
  27. this.checkArgCount(2);
  28. var left = this.operands[0].evaluate(doc);
  29. if(left !== undefined && left !== null) return left;
  30. var right = this.operands[1].evaluate(doc);
  31. return right;
  32. };