ElemMatchObjectMatchExpression.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. var ArrayMatchingMatchExpression = require('./ArrayMatchingMatchExpression.js');
  3. var ElemMatchObjectMatchExpression = module.exports = function ElemMatchObjectMatchExpression(){
  4. base.call(this);
  5. }, klass = ElemMatchObjectMatchExpression, base = ArrayMatchingMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  6. proto._sub = undefined;
  7. /**
  8. *
  9. * Writes a debug string for this object
  10. * @method debugString
  11. * @param level
  12. *
  13. */
  14. proto.debugString = function debugString(level){
  15. return this._debugAddSpace( level ) + this.path() + " $elemMatch (obj) " + (this.getTag() ? this.getTag().debugString() : '') + this._exp._debugString( level + 1 );
  16. };
  17. /**
  18. *
  19. * Return 1 since we have a single "sub"
  20. * @method numChildren
  21. *
  22. */
  23. proto.numChildren = function numChildren(){
  24. return 1;
  25. };
  26. /**
  27. *
  28. * Return the _sub property
  29. * @method getChild
  30. * @param i
  31. *
  32. */
  33. proto.getChild = function getChild(i){
  34. return this._sub.get();
  35. };
  36. /**
  37. *
  38. * Initialize the necessary items
  39. * @method init
  40. * @param path
  41. * @param type
  42. *
  43. */
  44. proto.init = function init(path, sub){
  45. this._sub = sub;
  46. return this.initPath(path);
  47. };
  48. /**
  49. *
  50. * Check if one of the items in the input array matches _sub
  51. * @method matchesArray
  52. * @param anArray
  53. * @param details
  54. *
  55. */
  56. proto.matchesArray = function matchesArray(anArray, details){
  57. for (var i in anArray) {
  58. var inner = anArray[i];
  59. if (!(inner instanceof Object))
  60. continue;
  61. if (this._sub.matchesJSON(inner, null)) {
  62. if (details && details.needRecord()) {
  63. details.setElemMatchKey(i);
  64. }
  65. return true;
  66. }
  67. }
  68. return false;
  69. };
  70. /**
  71. *
  72. * clone this instance to a new one
  73. * @method shallowClone
  74. *
  75. */
  76. proto.shallowClone = function shallowClone(){
  77. var element = new ElemMatchObjectMatchExpression();
  78. element.init(this.path(), this._sub.shallowClone());
  79. if ( this.getTag() ){
  80. element.setTag(this.getTag().clone());
  81. }
  82. return element;
  83. };