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?

Sayak Mukhopadhyay
  • 1,332
  • 2
  • 19
  • 34

1 Answers1

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

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • `maxTime` sets the timeout while processing. I want to set the idle cursor timeout. Reason why I want to set the `cursorTimeoutMillis` instead. – Sayak Mukhopadhyay Oct 18 '18 at 16:59
  • Sorry I haven't understood correctly your question. I've performed an edit – Orelsanpls Oct 18 '18 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. – Sayak Mukhopadhyay Oct 19 '18 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](https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/) and [here](https://docs.mongodb.com/manual/reference/built-in-roles/). If you get any trouble with the rights, open a new question, you i'll answers really fast on that one :) – Orelsanpls Oct 19 '18 at 11:43
  • > and check this [src](https://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized), if you get `MongoDB - admin user not authorized` error ! – fetahokey Nov 27 '21 at 21:00