TylerBrock / mongo-hacker

MongoDB Shell Enhancements for Hackers
tylerbrock.github.io/mongo-hacker
MIT License
1.79k stars 235 forks source link

failedToParse: The 'cursor' option is required, except for aggregate with the explain argument #182

Closed gianpaj closed 5 years ago

gianpaj commented 6 years ago
$ mongo
MongoDB shell version v3.6.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.0
Mongo-Hacker 0.0.14

> db.coll.insert({string: 'hello world1 world2'} )

> db.coll.aggregate([{ $project: { location: { $indexOfBytes: ["$string", "world"] } } }])
printStackTrace@src/mongo/shell/utils.js:74:15
DBCollection.prototype.aggregate@/Users/gianfranco/.mongorc.js:98:13
@(shell):1:1

2017-12-17T18:49:20.838+0200 E QUERY    [thread1] uncaught exception: aggregate failed: {
  "ok": 0,
  "errmsg": "The 'cursor' option is required, except for aggregate with the explain argument",
  "code": 9,
  "codeName": "FailedToParse"
}

Issue also with aggregate() function without the array.

> db.coll.aggregate().project({ location: { $indexOfBytes: ["$string", "world"] } })
printStackTrace@src/mongo/shell/utils.js:74:15
Aggregation.prototype.execute@/Users/gianfranco/.mongorc.js:152:9
Aggregation.prototype.shellPrint@/Users/gianfranco/.mongorc.js:170:9
shellPrintHelper@src/mongo/shell/utils.js:539:1
@(shell2):1:1

2017-12-17T18:56:54.026+0200 E QUERY    [thread1] uncaught exception: aggregation failed: {
  "ok": 0,
  "errmsg": "The 'cursor' option is required, except for aggregate with the explain argument",
  "code": 9,
  "codeName": "FailedToParse"
}
$ mongo --norc

> db.coll.aggregate([{ $project: { location: { $indexOfBytes: ["$string", "world"] } } }])
{ "_id" : ObjectId("5a36a07f36f314ee708bdf8b"), "location" : 6 }

same issue with mongo shell 3.4.3 btw

Looks like the issue is also(?) related to the server version

TylerBrock commented 6 years ago

Got it, thanks for the detailed bug report!

mov-q commented 6 years ago

any fix for this issue? experiencing the same problem with latest stable 3.6.x mongodb and latest mongo-hacker

gustawdaniel commented 6 years ago

I have the same problem:

This is my query

db.contracts.aggregate([{ $lookup: { from: 'machines', localField: '_id', foreignField: 'contract_id', as: 'machine' } }, { $unwind: { path: '$machine', preserveNullAndEmptyArrays: true } }, { $lookup: { from: 'contract_elements', localField: 'machine._id', foreignField: 'machine_id', as: 'contract_element' } }, { $unwind: { path: '$contract_element', preserveNullAndEmptyArrays: true } } ]).pretty()

Without this package it works correctly. When I installed package and typed mongo I saw

MongoDB server version: 3.6.4
Mongo-Hacker 0.0.14

And the same query caused error:

printStackTrace@src/mongo/shell/utils.js:74:15
DBCollection.prototype.aggregate@/home/daniel/.mongorc.js:551:13
@(shell):1:1

2018-04-18T06:33:32.119+0200 E QUERY    [thread1] uncaught exception: aggregate failed: {
  "ok": 0,
  "errmsg": "The 'cursor' option is required, except for aggregate with the explain argument",
  "code": 9,
  "codeName": "FailedToParse"
}

I am using Ubuntu and version of mongo from his repository.

mongo --version
MongoDB shell version v3.6.4
git version: d0181a711f7e7f39e60b5aeb1dc7097bf6ae5856
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64

Further searching:

db.contracts.find({})

Works correctly but

db.contracts.aggregate([])

finish with error:

printStackTrace@src/mongo/shell/utils.js:74:15
Aggregation.prototype.execute@/home/daniel/.mongorc.js:605:9
Aggregation.prototype.shellPrint@/home/daniel/.mongorc.js:623:9
shellPrintHelper@src/mongo/shell/utils.js:549:1
@(shell2):1:1

2018-04-18T06:41:32.746+0200 E QUERY    [thread1] uncaught exception: aggregation failed: {
  "ok": 0,
  "errmsg": "The 'cursor' option is required, except for aggregate with the explain argument",
  "code": 9,
  "codeName": "FailedToParse"
}
creative-cranels commented 6 years ago

@gustawdaniel Hi! Any news about cursor error? My prod app crashed :(

stennie commented 6 years ago

@kayoshi @JimmyLonely Can you clarify what you mean by "prod app crashed"? mongo-hacker is an extension for the mongo shell and should not affect your MongoDB server. The errors reported earlier in this issue are client errors.

If you suspect compatibility problems with mongo-hacker, try starting your shell with mongo --norc to ignore any extensions.

arun-topagi commented 6 years ago

@gianpaj Hye I am using MongoDb: 3.6.6 and mongoose 4.7.2, I getting same issue, If I use cursor({ batchSize: 1000 }) it's going to resolve and reject part.

gianpaj commented 6 years ago

@AKArunkumar I'm not sure where you can define the cursor's batch size.

I made an initial PR to fix one part of this: https://github.com/TylerBrock/mongo-hacker/pull/191

Instead of using:

db.collection.runCommand("aggregate", cmd);

I'm using:

var cmdObj = db.collection._makeCommand("aggregate", cmd);

return db.collection._db._runAggregate(cmdObj, extraOpts);

BTW I'm not sure if those functions are compatible with all version of the server and/or client.

salty-horse commented 6 years ago

In my foolishness I thought this was a MongoDB bug.

wolph commented 5 years ago

I think there's an easier fix, I've simply added a few lines after: https://github.com/TylerBrock/mongo-hacker/blob/f15ce3ffc9164be1b7d825e219c85fd2c8936420/hacks/aggregation.js#L17-L19

So it now looks like:

if (extraOpts === undefined) {
    extraOpts = {};
}

if (extraOpts.cursor === undefined){
    extraOpts.cursor = {batchSize: 1000};
}

var cmd = {pipeline: arr};
Object.extend(cmd, extraOpts);
gianpaj commented 5 years ago

@WoLpH awesome!

That's a much better bug fix!

Do you want to send PR? Otherwise I'm happy to

wolph commented 5 years ago

I've created a pull request :)