2

Since version 3.6 MongoDB requires the use of cursor or explain in aggregate queries. It's a breaking change so I have to modify my earlier code.

But when I add cursor or explain to my query, the request simply enters an endless loop and MongoDB never responds. It doesn't even seem to time out.

This simple aggregation just hangs the code and never responds:

db.collection('users').aggregate([{ $match: { username: 'admin' }}],
                                  { cursor: {} },
                                  (err, docs) => {
                                       console.log('Aggregation completed.');
                                  });

I can replace { cursor: {} } with { explain: true } and the result is the same. It works perfectly under older MongoDB versions without this one parameter.

Without cursor or explain I get this error message:

The 'cursor' option is required, except for aggregate with the explain argument

I'm not the only one who ran into this: https://github.com/nosqlclient/nosqlclient/issues/419

Tamás Polgár
  • 2,082
  • 5
  • 21
  • 48
  • There is no that requirement. Upgrade from 3.2 to 3.6 did not required any changes in my PHP project. I think that you mismatched something https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/#db.collection.aggregate – Neodan Jun 22 '18 at 06:49
  • Could you post a link where this requirement comes from? – Alex Blex Jun 22 '18 at 08:57
  • If I remove cursor, MongoDB returns the following error message: "The 'cursor' option is required, except for aggregate with the explain argument" – Tamás Polgár Jun 22 '18 at 15:25

1 Answers1

2

OK, this was a little tricky, but finally it works. Looks like there are some major breaking changes in MongoDB's Node.js driver which nobody bothered to tell me.

1. The Node.js MongoDB driver has to be upgraded. My current version is 3.0.7.

2. The way how MongoDB connects has been changed, breaking any old code. The client connection now returns a client object, not merely the db. It has to be handled differently. There is a SO answer explaining it perfectly:

db.collection is not a function when using MongoClient v3.0

3. Aggregations now return an AggregationCursor object, not an array of data. Instead of a callback now you have to iterate through it.

var cursor = collection.aggregate([ ... ], 
                                  { cursor: { batchSize: 1 } });

cursor.each((err, docs) => {
  ...
});

So it seems you have to rewrite ALL your db operations after upgrading to MongoDB 3.6. Yay, thanks for all the extra work, MongoDB Team! Guess that's where I'm done with your product.

Tamás Polgár
  • 2,082
  • 5
  • 21
  • 48