8

it might be obvious, but right now I'm not able to either find it in the docs or google it...

I'm using mongodb with the nodejs-driver and have a potentially long operation (> 10 minutes) pertaining to a cursor which does get a timeout (as specified in http://docs.mongodb.org/manual/core/cursors/#cursor-behaviors).

In the nodejs-driver API Documentation (http://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html) a method addCursorFlag(flag, value) is mentioned to be called on a Cursor.

However, there's no example on how to do it, and simply calling e.g.

objectCollection.find().limit(objectCount).addCursorFlag('noCursorTimeout', true).toArray(function (err, objects) {
    ...
}

leads to a TypeError: Object #<Cursor> has no method 'addCursorFlag'.

So how to go about making this Cursor exist longer than those 10 minutes?

Moreover, as required by the mongodb documentation, how do I then manually close the cursor?

Thanks!

2 Answers 2

13

The example you've provided:

db.collection.find().addCursorFlag('noCursorTimeout',true)

..works fine for me on driver version 2.14.21. I've an open cursor for 45 minutes now.

Could it be you were using 1.x NodeJS driver?

2
  • Works on version 3.6.0 in 2020
    – squirtgun
    Commented Aug 29, 2020 at 20:17
  • Tested with also with ^3.6.9. Works perfect. Thanks! Commented Jul 10, 2021 at 22:14
4

so I've got a partial solution for my problem. it's doesn't say so in the API docs, but apparently you have to specify it in the find() options like so:

objectCollection.find({},{timeout: false}).limit(objectCount).toArray(function (err, objects) {
    ...
}

however still, what about the cleanup? do those cursors ever get killed? is a call to db.close() sufficient?

2
  • 1
    Yes, call the close function to clean up the cursor. The cursor on the server should still be closed automatically by the driver once the cursor on the client leaves scope and is cleaned up by Node. But if it doesn't leave scope somehow and isn't closed, the server-side cursor will stay open indefinitely, occupying resources. I'd avoid an immortal cursor if possible.
    – wdberkeley
    Commented Feb 2, 2015 at 20:10
  • Thanks @wdberkeley ... just the info I needed. Commented Feb 4, 2015 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.