mongodb connection timeout error with mongoose and nodejs
Ok. I figured out the problem using this really helpful discussion.The default socket connection time for MongoDB is 30 seconds. If any query/operation takes longer than this the connection is aborted and connection timeout error occurs.With this change I was able to upload a 32GB file to GridFS without any interruption.
https://github.com/Automattic/mongoose/issues/4789
I was passing the timeout parameter in following way.
server: { socketOptions: { socketTimeoutMS: 3000000, connectionTimeoutMS: 3000000, keepAlive:3000000 } }, replset: { socketOptions: { keepAlive: 3000000, connectTimeoutMS: 3000000 } }};
But it needs to be set in the following way:
const serverOptions = { poolSize: 100, socketOptions: { socketTimeoutMS: 6000000 }};mongoose.createConnection(dbpath, { server: serverOptions, replset: serverOptions //if you are using replication});
In my case I have used localhost.
const serverOptions = { poolsize:100 , socketOptions:{ socketTimeoutMS: 6000000 } }; var mongodbUri = 'mongodb://localhost:27017/gridFS'; mongoose.connect(mongodbUri, { server: serverOptions });
Hope this will help anyone with similar issue.