IfNullExpression.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var IfNullExpression = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * An $ifNull pipeline expression.
  6. *
  7. * @see evaluate
  8. * @class IfNullExpression
  9. * @namespace mungedb.aggregate.pipeline.expressions
  10. * @module mungedb-aggregate
  11. * @constructor
  12. **/
  13. var klass = function IfNullExpression(){
  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 "$ifNull";
  20. };
  21. proto.addOperand = function addOperand(expr) {
  22. this.checkArgLimit(2);
  23. base.prototype.addOperand.call(this, expr);
  24. };
  25. /**
  26. * Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] }
  27. * @method evaluate
  28. **/
  29. proto.evaluate = function evaluate(doc){
  30. this.checkArgCount(2);
  31. var left = this.operands[0].evaluate(doc);
  32. if(left !== undefined && left !== null) return left;
  33. var right = this.operands[1].evaluate(doc);
  34. return right;
  35. };
  36. return klass;
  37. })();