Skip to content

Commit 6987049

Browse files
h3n4lclaude
andauthored
fix(mongodb): make method arguments optional where mongosh allows empty calls (#54)
Several mongosh methods can be called with no arguments, but the parser was requiring arguments. This fixes the grammar to match actual mongosh behavior: - find/findOne: support filter + projection args (arguments?) - aggregate: allow empty pipeline (arguments?) - Cursor methods: sort, collation, comment, hint, max, min, readConcern, returnKey, showRecordId, projection - all accept empty calls Verified against local mongosh instance. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a57d4b6 commit 6987049

3 files changed

Lines changed: 1205 additions & 1032 deletions

File tree

mongodb/MongoShellParser.g4

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,14 @@ methodCall
248248
;
249249

250250
// Specific method rules for better AST structure
251+
// find(filter?, projection?)
251252
findMethod
252-
: FIND LPAREN argument? RPAREN
253+
: FIND LPAREN arguments? RPAREN
253254
;
254255

256+
// findOne(filter?, projection?)
255257
findOneMethod
256-
: FIND_ONE LPAREN argument? RPAREN
258+
: FIND_ONE LPAREN arguments? RPAREN
257259
;
258260

259261
// countDocuments(filter?, options?)
@@ -271,9 +273,9 @@ distinctMethod
271273
: DISTINCT LPAREN arguments RPAREN
272274
;
273275

274-
// aggregate(pipeline, options?)
276+
// aggregate(pipeline?, options?)
275277
aggregateMethod
276-
: AGGREGATE LPAREN arguments RPAREN
278+
: AGGREGATE LPAREN arguments? RPAREN
277279
;
278280

279281
// getIndexes()
@@ -401,8 +403,9 @@ latencyStatsMethod
401403
: LATENCY_STATS LPAREN argument? RPAREN
402404
;
403405

406+
// sort(specification?) - can be called without args
404407
sortMethod
405-
: SORT LPAREN document RPAREN
408+
: SORT LPAREN document? RPAREN
406409
;
407410

408411
limitMethod
@@ -418,8 +421,9 @@ countMethod
418421
: COUNT LPAREN RPAREN
419422
;
420423

424+
// projection(doc?) - can be called without args
421425
projectionMethod
422-
: (PROJECTION | PROJECT) LPAREN document RPAREN
426+
: (PROJECTION | PROJECT) LPAREN document? RPAREN
423427
;
424428

425429
// Cursor methods
@@ -431,12 +435,14 @@ closeMethod
431435
: CLOSE LPAREN RPAREN
432436
;
433437

438+
// collation(doc?) - can be called without args
434439
collationMethod
435-
: COLLATION LPAREN document RPAREN
440+
: COLLATION LPAREN document? RPAREN
436441
;
437442

443+
// comment(str?) - can be called without args
438444
commentMethod
439-
: COMMENT LPAREN stringLiteral RPAREN
445+
: COMMENT LPAREN stringLiteral? RPAREN
440446
;
441447

442448
explainMethod
@@ -451,8 +457,9 @@ hasNextMethod
451457
: HAS_NEXT LPAREN RPAREN
452458
;
453459

460+
// hint(indexSpec?) - can be called without args
454461
hintMethod
455-
: HINT LPAREN argument RPAREN
462+
: HINT LPAREN argument? RPAREN
456463
;
457464

458465
isClosedMethod
@@ -471,8 +478,9 @@ mapMethod
471478
: MAP LPAREN argument RPAREN
472479
;
473480

481+
// max(indexBounds?) - can be called without args
474482
maxMethod
475-
: MAX LPAREN document RPAREN
483+
: MAX LPAREN document? RPAREN
476484
;
477485

478486
maxAwaitTimeMSMethod
@@ -483,8 +491,9 @@ maxTimeMSMethod
483491
: MAX_TIME_MS LPAREN NUMBER RPAREN
484492
;
485493

494+
// min(indexBounds?) - can be called without args
486495
minMethod
487-
: MIN LPAREN document RPAREN
496+
: MIN LPAREN document? RPAREN
488497
;
489498

490499
nextMethod
@@ -503,20 +512,23 @@ prettyMethod
503512
: PRETTY LPAREN RPAREN
504513
;
505514

515+
// readConcern(doc?) - can be called without args
506516
readConcernMethod
507-
: READ_CONCERN LPAREN document RPAREN
517+
: READ_CONCERN LPAREN document? RPAREN
508518
;
509519

510520
readPrefMethod
511521
: READ_PREF LPAREN arguments RPAREN
512522
;
513523

524+
// returnKey(bool?) - can be called without args
514525
returnKeyMethod
515-
: RETURN_KEY LPAREN (TRUE | FALSE) RPAREN
526+
: RETURN_KEY LPAREN (TRUE | FALSE)? RPAREN
516527
;
517528

529+
// showRecordId(bool?) - can be called without args
518530
showRecordIdMethod
519-
: SHOW_RECORD_ID LPAREN (TRUE | FALSE) RPAREN
531+
: SHOW_RECORD_ID LPAREN (TRUE | FALSE)? RPAREN
520532
;
521533

522534
sizeMethod
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test cursor methods with optional arguments
2+
3+
// find and findOne with multiple args
4+
db.coll.find({}, {a: 1})
5+
db.coll.findOne({name: "test"}, {_id: 0})
6+
7+
// Empty cursor methods
8+
db.coll.find().sort()
9+
db.coll.find().collation()
10+
db.coll.find().comment()
11+
db.coll.find().hint()
12+
db.coll.find().max()
13+
db.coll.find().min()
14+
db.coll.find().readConcern()
15+
db.coll.find().returnKey()
16+
db.coll.find().showRecordId()
17+
db.coll.find().projection()
18+
19+
// aggregate with empty pipeline
20+
db.coll.aggregate()

0 commit comments

Comments
 (0)