3

I am using mongoose (4.13.10) in a nodeJs server to connect to a mongodb instance using

mongoose.createConnection('mongodb://user:pass@localhost:27017/mydb')

I am using cursors to iterate over the records of my database and making multiple queries to the database which takes a lot of time and thus I am getting Cursor not found exceptions. I have found that these happen due to the idle timeout of cursors which matches my observations.

Now I don't want to flag nocursortimeout as true and instead set the timeout to some high value by which I am sure the long running queries will be done. But I am unable to find any documentation or guide how to set the the cursorTimeoutMillis server parameter.

How do I set this parameter? If possible I would like to set it for a particular cursor and not on the whole connection. Is it possible?

1 Answer 1

3

You can set the cursorTimeoutMillis option using mongodb-native-client, like :

const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/test', (err, db) => {
  // Use the admin database for the operation
  const adminDb = db.admin();

  // Set the cursor timeout millis option
  adminDb.command({
    setParameter: 1,
    cursorTimeoutMillis: <num>
  }, function(err, info) {
    // Close the connection
    db.close();
  });
});

From mongoose, like :

YourModel.db.db.admin().command({}, (err, res) => {
    console.log(res);
});

To set it directly at the database run :

mongod --setParameter cursorTimeoutMillis=300000

Docs

http://mongodb.github.io/node-mongodb-native/2.0/api/Admin.html#command

How to run raw mongoDB commands using mongoose?

https://docs.mongodb.com/manual/reference/parameters/#param.cursorTimeoutMillis

5
  • maxTime sets the timeout while processing. I want to set the idle cursor timeout. Reason why I want to set the cursorTimeoutMillis instead. Oct 18, 2018 at 16:59
  • Sorry I haven't understood correctly your question. I've performed an edit
    – Orelsanpls
    Oct 18, 2018 at 17:52
  • Thanks for the solution. It seems like it would work but I am stuck trying to authenticate against the admin database. The user authenticating the initial connection has no perms in the admin data (obviously) and thus I am unable to admin().command(). I have also checked that the setParameter command has to be performed against admin db. Oct 19, 2018 at 10:09
  • Maybe you could create an other user having the admin rights or gives admin right to your current user. Look at here and here. If you get any trouble with the rights, open a new question, you i'll answers really fast on that one :)
    – Orelsanpls
    Oct 19, 2018 at 11:43
  • > and check this src, if you get MongoDB - admin user not authorized error !
    – fetahokey
    Nov 27, 2021 at 21:00

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.