MatchExpressionParser.js 22 KB

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