MatchExpressionParser.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. "use strict";
  2. // File: expression_parser.cpp
  3. var MatchExpressionParser = module.exports = function (){
  4. }, klass = MatchExpressionParser, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  5. // DEPENDENCIES
  6. var errors = require("../../Errors.js"),
  7. ErrorCodes = errors.ErrorCodes,
  8. AndMatchExpression = require("./AndMatchExpression.js"),
  9. MatchExpression = require("./MatchExpression.js"),
  10. OrMatchExpression = require("./OrMatchExpression.js"),
  11. ModMatchExpression = require("./ModMatchExpression.js"),
  12. NorMatchExpression = require("./NorMatchExpression.js"),
  13. NotMatchExpression = require("./NotMatchExpression.js"),
  14. LTMatchExpression = require("./LTMatchExpression.js"),
  15. LTEMatchExpression = require("./LTEMatchExpression.js"),
  16. GTMatchExpression = require("./GTMatchExpression.js"),
  17. GTEMatchExpression = require("./GTEMatchExpression.js"),
  18. InMatchExpression = require("./InMatchExpression.js"),
  19. SizeMatchExpression = require("./SizeMatchExpression.js"),
  20. TypeMatchExpression = require("./TypeMatchExpression.js"),
  21. ExistsMatchExpression = require("./ExistsMatchExpression.js"),
  22. EqualityMatchExpression = require("./EqualityMatchExpression.js"),
  23. ArrayMatchingMatchExpression = require("./ArrayMatchingMatchExpression.js"),
  24. RegexMatchExpression = require("./RegexMatchExpression.js"),
  25. FalseMatchExpression = require("./FalseMatchExpression.js"),
  26. ComparisonMatchExpression = require("./ComparisonMatchExpression.js"),
  27. ElemMatchValueMatchExpression = require("./ElemMatchValueMatchExpression.js"),
  28. ElemMatchObjectMatchExpression = require("./ElemMatchObjectMatchExpression.js"),
  29. AllElemMatchOp = require("./AllElemMatchOp.js"),
  30. AtomicMatchExpression = require("./AtomicMatchExpression.js");
  31. // The maximum allowed depth of a query tree. Just to guard against stack overflow.
  32. var MAXIMUM_TREE_DEPTH = 100;
  33. /**
  34. *
  35. * Check if the input element is an expression
  36. * @method _isExpressionDocument
  37. * @param element
  38. *
  39. */
  40. proto._isExpressionDocument = function _isExpressionDocument(element, allowIncompleteDBRef){
  41. if (!(element instanceof Object))
  42. return false;
  43. if (Object.keys(element).length === 0)
  44. return false;
  45. var name = Object.keys(element)[0];
  46. if (name[0] != '$')
  47. return false;
  48. if (this._isDBRefDocument(element, allowIncompleteDBRef))
  49. return false;
  50. return true;
  51. };
  52. proto._isDBRefDocument = function _isDBRefDocument(obj, allowIncompleteDBRef) {
  53. var hasRef, hasID, hasDB = false;
  54. var i, fieldName, element = null,
  55. keys = Object.keys(obj), length = keys.length;
  56. for (var i = 0; i < length; i++) {
  57. var fieldName = keys[i],
  58. element = obj[fieldName];
  59. if (!hasRef && fieldName === '$ref')
  60. hasRef = true;
  61. else if (!hasID && fieldName === '$id')
  62. hasID = true;
  63. else if (!hasDB && fieldName === '$db')
  64. hasDB = true;
  65. }
  66. return allowIncompleteDBRef && (hasRef || hasID || hasDB) || (hasRef && hasID);
  67. };
  68. /**
  69. *
  70. * Parse the input object into individual elements
  71. * @method _parse
  72. * @param obj
  73. * @param level
  74. *
  75. */
  76. proto._parse = function _parse(obj, level){
  77. if (level > MAXIMUM_TREE_DEPTH)
  78. return {code:ErrorCodes.BAD_VALUE, description:"exceeded maximum query tree depth of " +
  79. MAXIMUM_TREE_DEPTH + " at " + obj};
  80. var rest, temp, status, element, eq, real;
  81. var root = new AndMatchExpression();
  82. var objkeys = Object.keys(obj);
  83. var currname, currval;
  84. var topLevel = level === 0;
  85. level++;
  86. for (var i = 0; i < objkeys.length; i++) {
  87. currname = objkeys[i];
  88. currval = obj[currname];
  89. if (currname[0] == '$' ) {
  90. rest = currname.substr(1);
  91. // TODO: optimize if block?
  92. if ("or" == rest) {
  93. if (!(currval instanceof Array))
  94. return {code:ErrorCodes.BAD_VALUE, description:"$or needs an array"};
  95. temp = new OrMatchExpression();
  96. status = this._parseTreeList(currval, temp, level);
  97. if (status.code != ErrorCodes.OK)
  98. return status;
  99. root.add(temp);
  100. }
  101. else if ("and" == rest) {
  102. if (!(currval instanceof Array))
  103. return {code:ErrorCodes.BAD_VALUE, description:"and needs an array"};
  104. temp = new AndMatchExpression();
  105. status = this._parseTreeList(currval, temp, level);
  106. if (status.code != ErrorCodes.OK)
  107. return status;
  108. root.add(temp);
  109. }
  110. else if ("nor" == rest) {
  111. if (!(currval instanceof Array))
  112. return {code:ErrorCodes.BAD_VALUE, description:"and needs an array"};
  113. temp = new NorMatchExpression();
  114. status = this._parseTreeList(currval, temp, level);
  115. if (status.code != ErrorCodes.OK)
  116. return status;
  117. root.add(temp);
  118. }
  119. else if (("atomic" == rest) || ("isolated" == rest)) {
  120. if (!topLevel)
  121. return {code:ErrorCodes.BAD_VALUE, description:"$atomic/$isolated has to be at the top level"};
  122. if (element)
  123. root.add(new AtomicMatchExpression());
  124. }
  125. else if ("where" == rest) {
  126. /*
  127. if ( !topLevel )
  128. return StatusWithMatchExpression( ErrorCodes::BAD_VALUE, "$where has to be at the top level" );
  129. */
  130. return {'code':'FAILED_TO_PARSE', 'desc':'Where unimplimented.'};
  131. /*
  132. status = this.expressionParserWhereCallback(element);
  133. if (status.code != ErrorCodes.OK)
  134. return status;
  135. root.add(status.result);*/
  136. }
  137. else if ("comment" == rest) {
  138. 1+1;
  139. }
  140. else {
  141. return {code:ErrorCodes.BAD_VALUE, description:"unknown top level operator: " + currname};
  142. }
  143. continue;
  144. }
  145. if (this._isExpressionDocument(currval)) {
  146. status = this._parseSub(currname, currval, root, level);
  147. if (status.code != ErrorCodes.OK)
  148. return status;
  149. continue;
  150. }
  151. if (currval instanceof RegExp) {
  152. status = this._parseRegexElement(currname, currval);
  153. if (status.code != ErrorCodes.OK)
  154. return status;
  155. root.add(status.result);
  156. continue;
  157. }
  158. eq = new EqualityMatchExpression();
  159. status = eq.init(currname, currval);
  160. if (status.code != ErrorCodes.OK)
  161. return status;
  162. root.add(eq);
  163. }
  164. if (root.numChildren() == 1) {
  165. return {code:ErrorCodes.OK, result:root.getChild(0)};
  166. }
  167. return {code:ErrorCodes.OK, result:root};
  168. };
  169. /**
  170. *
  171. * Parse the $all element
  172. * @method _parseAll
  173. * @param name
  174. * @param element
  175. *
  176. */
  177. proto._parseAll = function _parseAll(name, element, level){
  178. var status, i;
  179. if (!(element instanceof Array))
  180. return {code:ErrorCodes.BAD_VALUE, description:"$all needs an array"};
  181. var arr = element;
  182. if ((arr[0] instanceof Object) && ("$elemMatch" == Object.keys(arr[0])[0])) {
  183. // $all : [ { $elemMatch : {} } ... ]
  184. var temp = new AllElemMatchOp();
  185. status = temp.init(name);
  186. if (status.code != ErrorCodes.OK)
  187. return status;
  188. for (i = 0; i < arr.length; i++) {
  189. var hopefullyElemMatchElement = arr[i];
  190. if (!(hopefullyElemMatchElement instanceof Object)) {
  191. // $all : [ { $elemMatch : ... }, 5 ]
  192. return {code:ErrorCodes.BAD_VALUE, description:"$all/$elemMatch has to be consistent"};
  193. }
  194. if ("$elemMatch" != Object.keys(hopefullyElemMatchElement)[0]) {
  195. // $all : [ { $elemMatch : ... }, { x : 5 } ]
  196. return {code:ErrorCodes.BAD_VALUE, description:"$all/$elemMatch has to be consistent"};
  197. }
  198. status = this._parseElemMatch("", hopefullyElemMatchElement.$elemMatch, level);
  199. if (status.code != ErrorCodes.OK)
  200. return status;
  201. temp.add(status.result);
  202. }
  203. return {code:ErrorCodes.OK, result:temp};
  204. }
  205. var myAnd = new AndMatchExpression();
  206. for (i = 0; i < arr.length; i++) {
  207. var e = arr[i];
  208. if (e instanceof RegExp) {
  209. var r = new RegexMatchExpression();
  210. status = r.init(name, e);
  211. if (status.code != ErrorCodes.OK)
  212. return status;
  213. myAnd.add(r);
  214. }
  215. else if ((e instanceof Object) && (typeof(Object.keys(e)[0] == 'string' && Object.keys(e)[0][0] == '$' ))) {
  216. return {code:ErrorCodes.BAD_VALUE, description:"no $ expressions in $all"};
  217. }
  218. else {
  219. var x = new EqualityMatchExpression();
  220. status = x.init(name, e);
  221. if (status.code != ErrorCodes.OK)
  222. return status;
  223. myAnd.add(x);
  224. }
  225. }
  226. if (myAnd.numChildren() === 0) {
  227. return {code:ErrorCodes.OK, result:new FalseMatchExpression()};
  228. }
  229. return {code:ErrorCodes.OK, result:myAnd};
  230. };
  231. /**
  232. *
  233. * Parse the input array and add new RegexMatchExpressions to entries
  234. * @method _parseArrayFilterEntries
  235. * @param entries
  236. * @param theArray
  237. *
  238. */
  239. proto._parseArrayFilterEntries = function _parseArrayFilterEntries(entries, theArray){
  240. var status, e, r;
  241. for (var i = 0; i < theArray.length; i++) {
  242. e = theArray[i];
  243. if (this._isExpressionDocument(e, false)) {
  244. return {code:ErrorCodes.BAD_VALUE, description:"cannot nest $ under $in"};
  245. }
  246. if (e instanceof RegExp ) {
  247. r = new RegexMatchExpression();
  248. status = r.init("", e);
  249. if (status.code != ErrorCodes.OK)
  250. return status;
  251. status = entries.addRegex(r);
  252. if (status.code != ErrorCodes.OK)
  253. return status;
  254. }
  255. else {
  256. status = entries.addEquality(e);
  257. if (status.code != ErrorCodes.OK)
  258. return status;
  259. }
  260. }
  261. return {code:ErrorCodes.OK};
  262. };
  263. /**
  264. *
  265. * Parse the input ComparisonMatchExpression
  266. * @method _parseComparison
  267. * @param name
  268. * @param cmp
  269. * @param element
  270. *
  271. */
  272. proto._parseComparison = function _parseComparison(name, cmp, element){
  273. var temp = new ComparisonMatchExpression(cmp);
  274. var status = temp.init(name, element);
  275. if (status.code != ErrorCodes.OK)
  276. return status;
  277. return {code:ErrorCodes.OK, result:temp};
  278. };
  279. /**
  280. *
  281. * Parse an element match into the appropriate expression
  282. * @method _parseElemMatch
  283. * @param name
  284. * @param element
  285. *
  286. */
  287. proto._parseElemMatch = function _parseElemMatch(name, element, level){
  288. var temp, status;
  289. if (!(element instanceof Object))
  290. return {code:ErrorCodes.BAD_VALUE, description:"$elemMatch needs an Object"};
  291. // $elemMatch value case applies when the children all
  292. // work on the field 'name'.
  293. // This is the case when:
  294. // 1) the argument is an expression document; and
  295. // 2) expression is not a AND/NOR/OR logical operator. Children of
  296. // these logical operators are initialized with field names.
  297. // 3) expression is not a WHERE operator. WHERE works on objects instead
  298. // of specific field.
  299. var elt = element[Object.keys(element)[0]],
  300. isElemMatchValue = this._isExpressionDocument(element, true) &&
  301. elt !== '$and' &&
  302. elt !== '$nor' &&
  303. elt !== '$or' &&
  304. elt !== '$where';
  305. if (isElemMatchValue) {
  306. // value case
  307. var theAnd = new AndMatchExpression();
  308. status = this._parseSub("", element, theAnd, level);
  309. if (status.code != ErrorCodes.OK)
  310. return status;
  311. temp = new ElemMatchValueMatchExpression();
  312. status = temp.init(name);
  313. if (status.code != ErrorCodes.OK)
  314. return status;
  315. for (var i = 0; i < theAnd.numChildren(); i++ ) {
  316. temp.add(theAnd.getChild(i));
  317. }
  318. theAnd.clearAndRelease();
  319. return {code:ErrorCodes.OK, result:temp};
  320. }
  321. // DBRef value case
  322. // A DBRef document under a $elemMatch should be treated as an object case
  323. // because it may contain non-DBRef fields in addition to $ref, $id and $db.
  324. // object case
  325. status = this._parse(element, level);
  326. if (status.code != ErrorCodes.OK)
  327. return status;
  328. temp = new ElemMatchObjectMatchExpression();
  329. status = temp.init(name, status.result);
  330. if (status.code != ErrorCodes.OK)
  331. return status;
  332. return {code:ErrorCodes.OK, result:temp};
  333. };
  334. /**
  335. *
  336. * Parse a ModMatchExpression
  337. * @method _parseMOD
  338. * @param name
  339. * @param element
  340. *
  341. */
  342. proto._parseMOD = function _parseMOD(name, element){
  343. var d,r;
  344. if (!(element instanceof Array))
  345. return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, needs to be an array"};
  346. if (element.length < 2)
  347. return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, not enough elements"};
  348. if (element.length > 2)
  349. return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, too many elements"};
  350. if (typeof element[0] !== 'number') {
  351. return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, divisor not a number"};
  352. } else {
  353. d = element[0];
  354. }
  355. if (typeof element[1] !== 'number') {
  356. return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, remainder not a number"};
  357. } else {
  358. r = element[1];
  359. }
  360. var temp = new ModMatchExpression();
  361. var status = temp.init( name, d, r);
  362. if (status.code != ErrorCodes.OK)
  363. return status;
  364. return {code:ErrorCodes.OK, result:temp};
  365. };
  366. /**
  367. *
  368. * Parse a NotMatchExpression
  369. * @method _parseNot
  370. * @param name
  371. * @param element
  372. *
  373. */
  374. proto._parseNot = function _parseNot(name, element, level){
  375. var status;
  376. if (element instanceof RegExp) {
  377. status = this._parseRegexElement(name, element);
  378. if (status.code != ErrorCodes.OK)
  379. return status;
  380. var n = new NotMatchExpression();
  381. status = n.init(status.result);
  382. if (status.code != ErrorCodes.OK)
  383. return status;
  384. return {code:ErrorCodes.OK, result:n};
  385. }
  386. if (!(element instanceof Object))
  387. return {code:ErrorCodes.BAD_VALUE, result:"$not needs a regex or a document"};
  388. if (element == {})
  389. return {code:ErrorCodes.BAD_VALUE, result:"$not cannot be empty"};
  390. var theAnd = new AndMatchExpression();
  391. status = this._parseSub(name, element, theAnd, level);
  392. if (status.code != ErrorCodes.OK)
  393. return status;
  394. // TODO: this seems arbitrary?
  395. // tested in jstests/not2.js
  396. for (var i = 0; i < theAnd.numChildren(); i++) {
  397. if (theAnd.getChild(i).matchType == MatchExpression.REGEX) {
  398. return {code:ErrorCodes.BAD_VALUE, result:"$not cannot have a regex"};
  399. }
  400. }
  401. var theNot = new NotMatchExpression();
  402. status = theNot.init(theAnd);
  403. if (status.code != ErrorCodes.OK)
  404. return status;
  405. return {code:ErrorCodes.OK, result:theNot};
  406. };
  407. /**
  408. *
  409. * Parse a RegexMatchExpression
  410. * @method _parseRegexDocument
  411. * @param name
  412. * @param doc
  413. *
  414. */
  415. proto._parseRegexDocument = function _parseRegexDocument(name, doc){
  416. var regex = '', regexOptions = '', e;
  417. if(doc.$regex) {
  418. e = doc.$regex;
  419. if(e instanceof RegExp) {
  420. var str = e.toString(),
  421. flagIndex = 0;
  422. for (var c = str.length; c > 0; c--){
  423. if (str[c] == '/') {
  424. flagIndex = c;
  425. break;
  426. }
  427. }
  428. regex = (flagIndex? str : str.substr(1, flagIndex-1));
  429. regexOptions = str.substr(flagIndex, str.length);
  430. } else if (typeof e === 'string') {
  431. regex = e;
  432. } else {
  433. return {code:ErrorCodes.BAD_VALUE, description:"$regex has to be a string"};
  434. }
  435. }
  436. if(doc.$options) {
  437. e = doc.$options;
  438. if(typeof(e) === 'string') {
  439. regexOptions = e;
  440. } else {
  441. return {code:ErrorCodes.BAD_VALUE, description:"$options has to be a string"};
  442. }
  443. }
  444. var temp = new RegexMatchExpression();
  445. var status = temp.init(name, regex, regexOptions);
  446. if (status.code != ErrorCodes.OK)
  447. return status;
  448. return {code:ErrorCodes.OK, result:temp};
  449. };
  450. /**
  451. *
  452. * Parse an element into a RegexMatchExpression
  453. * @method _parseRegexElement
  454. * @param name
  455. * @param element
  456. *
  457. */
  458. proto._parseRegexElement = function _parseRegexElement(name, element){
  459. if (!(element instanceof RegExp))
  460. return {code:ErrorCodes.BAD_VALUE, description:"not a regex"};
  461. var str = element.toString(),
  462. flagIndex = 0;
  463. for (var c = str.length; c > 0; c--){
  464. if (str[c] == '/') {
  465. flagIndex = c;
  466. break;
  467. }
  468. }
  469. var regex = str.substr(1, flagIndex-1),
  470. regexOptions = str.substr(flagIndex+1, str.length),
  471. temp = new RegexMatchExpression(),
  472. status = temp.init(name, regex, regexOptions);
  473. if (status.code != ErrorCodes.OK)
  474. return status;
  475. return {code:ErrorCodes.OK, result:temp};
  476. };
  477. /**
  478. *
  479. * Parse a sub expression
  480. * @method _parseSub
  481. * @param name
  482. * @param sub
  483. * @param root
  484. *
  485. */
  486. proto._parseSub = function _parseSub(name, sub, root, level){
  487. var subkeys = Object.keys(sub),
  488. currname, currval;
  489. if (level > MAXIMUM_TREE_DEPTH) {
  490. return {code:ErrorCodes.BAD_VALUE, description:"exceeded maximum query tree depth of " +
  491. MAXIMUM_TREE_DEPTH + " at " + sub};
  492. }
  493. level++;
  494. // DERIVATION: We are not implementing Geo functions yet.
  495. for (var i = 0; i < subkeys.length; i++) {
  496. currname = subkeys[i];
  497. currval = sub[currname];
  498. var deep = {};
  499. deep[currname] = currval;
  500. var status = this._parseSubField(sub, root, name, deep, level);
  501. if (status.code != ErrorCodes.OK)
  502. return status;
  503. if (status.result)
  504. root.add(status.result);
  505. }
  506. return {code:ErrorCodes.OK, result:root};
  507. };
  508. /**
  509. *
  510. * Parse a sub expression field
  511. * @method _parseSubField
  512. * @param context
  513. * @param andSoFar
  514. * @param name
  515. * @param element
  516. *
  517. */
  518. proto._parseSubField = function _parseSubField(context, andSoFar, name, element, level){
  519. // TODO: these should move to getGtLtOp, or its replacement
  520. var currname = Object.keys(element)[0];
  521. var currval = element[currname];
  522. if ("$eq" == currname)
  523. return this._parseComparison(name, 'EQ', currval);
  524. if ("$not" == currname)
  525. return this._parseNot(name, currval, level);
  526. var status, temp, temp2;
  527. switch (currname) {
  528. // TODO: -1 is apparently a value for mongo, but we handle strings so...
  529. case '$lt':
  530. return this._parseComparison(name, 'LT', currval);
  531. case '$lte':
  532. return this._parseComparison(name, 'LTE', currval);
  533. case '$gt':
  534. return this._parseComparison(name, 'GT', currval);
  535. case '$gte':
  536. return this._parseComparison(name, 'GTE', currval);
  537. case '$ne':
  538. // Just because $ne can be rewritten as the negation of an
  539. // equality does not mean that $ne of a regex is allowed. See SERVER-1705.
  540. if (currval instanceof RegExp) {
  541. return {code:ErrorCodes.BAD_VALUE, description:"Can't have regex as arg to $ne."};
  542. }
  543. status = this._parseComparison(name, 'EQ', currval);
  544. if (status.code != ErrorCodes.OK)
  545. return status;
  546. var n = new NotMatchExpression();
  547. status = n.init(status.result);
  548. if (status.code != ErrorCodes.OK)
  549. return status;
  550. return {code:ErrorCodes.OK, result:n};
  551. case '$eq':
  552. return this._parseComparison(name, 'EQ', currval);
  553. case '$in':
  554. if (!(currval instanceof Array))
  555. return {code:ErrorCodes.BAD_VALUE, description:"$in needs an array"};
  556. temp = new InMatchExpression();
  557. status = temp.init(name);
  558. if (status.code != ErrorCodes.OK)
  559. return status;
  560. status = this._parseArrayFilterEntries(temp.getArrayFilterEntries(), currval);
  561. if (status.code != ErrorCodes.OK)
  562. return status;
  563. return {code:ErrorCodes.OK, result:temp};
  564. case '$nin':
  565. if (!(currval instanceof Array))
  566. return {code:ErrorCodes.BAD_VALUE, description:"$nin needs an array"};
  567. temp = new InMatchExpression();
  568. status = temp.init(name);
  569. if (status.code != ErrorCodes.OK)
  570. return status;
  571. status = this._parseArrayFilterEntries(temp.getArrayFilterEntries(), currval);
  572. if (status.code != ErrorCodes.OK)
  573. return status;
  574. temp2 = new NotMatchExpression();
  575. status = temp2.init(temp);
  576. if (status.code != ErrorCodes.OK)
  577. return status;
  578. return {code:ErrorCodes.OK, result:temp2};
  579. case '$size':
  580. var size = 0;
  581. if ( typeof(currval) === 'string')
  582. // matching old odd semantics
  583. size = 0;
  584. else if (typeof(currval) === 'number')
  585. // SERVER-11952. Setting 'size' to -1 means that no documents
  586. // should match this $size expression.
  587. if (currval < 0)
  588. size = -1;
  589. else
  590. size = currval;
  591. else {
  592. return {code:ErrorCodes.BAD_VALUE, description:"$size needs a number"};
  593. }
  594. // DERIVATION/Potential bug: Mongo checks to see if doube values are exactly equal to
  595. // their int converted version. If not, size = -1.
  596. temp = new SizeMatchExpression();
  597. status = temp.init(name, size);
  598. if (status.code != ErrorCodes.OK)
  599. return status;
  600. return {code:ErrorCodes.OK, result:temp};
  601. case '$exists':
  602. if (currval == {})
  603. return {code:ErrorCodes.BAD_VALUE, description:"$exists can't be eoo"};
  604. temp = new ExistsMatchExpression();
  605. status = temp.init(name);
  606. if (status.code != ErrorCodes.OK)
  607. return status;
  608. if (currval) // DERIVATION: This may have to check better than truthy? Need to look at TrueValue
  609. return {code:ErrorCodes.OK, result:temp};
  610. temp2 = new NotMatchExpression();
  611. status = temp2.init(temp);
  612. if (status.code != ErrorCodes.OK)
  613. return status;
  614. return {code:ErrorCodes.OK, result:temp2};
  615. case '$type':
  616. if (typeof(currval) != 'number')
  617. return {code:ErrorCodes.BAD_VALUE, description:"$type has to be a number"};
  618. var type = currval;
  619. temp = new TypeMatchExpression();
  620. status = temp.init(name, type);
  621. if (status.code != ErrorCodes.OK)
  622. return status;
  623. return {code:ErrorCodes.OK, result:temp};
  624. case '$mod':
  625. return this._parseMOD(name, currval);
  626. case '$options':
  627. // TODO: try to optimize this
  628. // we have to do this since $options can be before or after a $regex
  629. // but we validate here
  630. for(var i = 0; i < Object.keys(context).length; i++) {
  631. temp = Object.keys(context)[i];
  632. if (temp == '$regex')
  633. return {code:ErrorCodes.OK, result:null};
  634. }
  635. return {code:ErrorCodes.BAD_VALUE, description:"$options needs a $regex"};
  636. case '$regex':
  637. return this._parseRegexDocument(name, context);
  638. case '$elemMatch':
  639. return this._parseElemMatch(name, currval, level);
  640. case '$all':
  641. return this._parseAll(name, currval, level);
  642. case '$geoWithin':
  643. case '$geoIntersects':
  644. case '$near':
  645. case '$nearSphere':
  646. var x = 'Temporary value until Geo fns implimented.';
  647. return this.expressionParserGeoCallback(name, x, context);
  648. default:
  649. return {code:ErrorCodes.BAD_VALUE, description:"not handled: " + element};
  650. } // end switch
  651. return {code:ErrorCodes.BAD_VALUE, description:"not handled: " + element};
  652. };
  653. /**
  654. *
  655. * Parse a list of parsable elements
  656. * @method _parseTreeList
  657. * @param arr
  658. * @param out
  659. *
  660. */
  661. proto._parseTreeList = function _parseTreeList(arr, out, level){
  662. if (arr.length === 0)
  663. return {code:ErrorCodes.BAD_VALUE, description:"$and/$or/$nor must be a nonempty array"};
  664. var status, element;
  665. for (var i = 0; i < arr.length; i++) {
  666. element = arr[i];
  667. if (!(element instanceof Object))
  668. return {code:ErrorCodes.BAD_VALUE, description:"$or/$and/$nor entries need to be full objects"};
  669. status = this._parse(element, level);
  670. if (status.code != ErrorCodes.OK)
  671. return status;
  672. out.add(status.result);
  673. }
  674. return {code:ErrorCodes.OK};
  675. };
  676. /**
  677. *
  678. * Wrapper for _parse
  679. * @method parse
  680. * @param obj
  681. *
  682. */
  683. proto.parse = function parse(obj){
  684. return this._parse(obj, 0);
  685. };