title: mongodb connection timeout error with mongoose and nodejs
body: <p>I desperately need some help.I am trying to upload large file(8 GB) to gridfs using mongoose and nodeJS. But as the file is very large it takes some time to upload. And after a while I get the following error:</p>
  
  <pre><code>home/user/FileUpload/node_modules/mongodb/lib/utils.js:98
      process.nextTick(function() { throw err; });
                                    ^
  MongoError: connection 0 to 127.0.0.1:27017 timed out
      at Function.MongoError.create (/home/user/FileUpload/node_modules/mongodb-core/lib/error.js:29:11)
      at Socket.&lt;anonymous&gt; (/home/user/FileUpload/node_modules/mongodb-core/lib/connection/connection.js:186:20)
      at Object.onceWrapper (events.js:314:30)
      at emitNone (events.js:105:13)
      at Socket.emit (events.js:207:7)
      at Socket._onTimeout (net.js:402:8)
      at ontimeout (timers.js:488:11)
      at tryOnTimeout (timers.js:323:5)
      at Timer.listOnTimeout (timers.js:283:5)
  </code></pre>
  
  <p>I have tried to resolve this by increasing connectTimeoutMS but the error still persist. I am using MongoDB 3.4.5 mongoose 4.8.4 nodejs 8.1.4 and npm 5.0.3.</p>
  
  <p>Following is app.js:</p>
  
  <pre><code>var mongoose = require('mongoose');
  var schema = mongoose.schema;
  mongoose.connect('mongodb://127.0.0.1/gridFS'),{
      server: {
          socketOptions: {
              socketTimeoutMS: 3000000,
              connectionTimeoutMS: 3000000,
              keepAlive:3000000
          }
      },
  
      replset: {
          socketOptions: {
              keepAlive: 3000000,
              connectTimeoutMS: 3000000
          }
      }
  };
  var conn = mongoose.connection;
  var path = require('path');
  var Grid = require('gridfs-stream');
  var fs = require('fs');
  var videoPath = path.join(__dirname, 'readFrom/bio seq test1.txt');
  Grid.mongo = mongoose.mongo;
  conn.once('open', function(){
  
      console.log('- connection open -');
      var gfs = Grid(conn.db);
  
      var writestream = gfs.createWriteStream({
  
          filename: 'bio seq test 1'
      });
  
      fs.createReadStream(videoPath).pipe(writestream);
      writestream.on('close', function(file){
        console.log(file.filename + 'Written to DB');
      });
  });
  </code></pre>
  
  <p>Following is package.json file:</p>
  
  <pre><code>{
    "name": "file-upload-gridfs",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "echo '' &amp;amp;&amp;amp; exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
      "body-parser": "^1.16.1",
      "cookie-parser": "^1.4.3",
      "express": "^4.14.1",
      "gridfs-stream": "^1.1.1",
      "mongoose": "^4.8.4",
      "morgan": "^1.8.2",
      "multer": "1.3.0",
      "multer-gridfs-storage": "1.0.0",
      "path.join": "^1.0.0",
      "serve-favicon": "^2.4.3"
    }
  }
  </code></pre>