title: Reliably reconnect to MongoDB
body: <p><strong>UPDATE:</strong> I am using the 2.1 version on the driver, against 3.2</p>
  
  <p>I have a node application that uses MongoDB. The problem I have is that if the MongoDB server goes down for any reason, the application doesn't reconnect.
  To get this right, I based my tests on the code in <a href="http://mongodb.github.io/node-mongodb-native/2.0/tutorials/connection_failures/">this official tutorial</a>.</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient
    , f = require('util').format;
  
  MongoClient.connect('mongodb://localhost:27017/test', 
  
  // Optional: uncomment if necessary
  // { db: { bufferMaxEntries: 3 } },
  
  
  function(err, db) {
    var col = db.collection('t');
  
    setInterval(function() {
      col.insert({a:1}, function(err, r) {
        console.log("insert")
        console.log(err)
  
        col.findOne({}, function(err, doc) {
          console.log("findOne")
          console.log(err)
        });
      })
    }, 1000)
  });
  </code></pre>
  
  <p>The idea is to run this script, and then stop mongod, and then restart it.
  So, here we go:</p>
  
  <h1>TEST 1: stopping mongod for 10 seconds</h1>
  
  <p>Stopping MongoDb for 10 seconds does the desired result: it will stop running the queries for those 10 seconds, and then will run all of them once the server is back ip</p>
  
  <h1>TEST 2: stopping mongod for 30 seconds</h1>
  
  <p>After exactly 30 seconds, I start getting:</p>
  
  <pre><code>{ [MongoError: topology was destroyed] name: 'MongoError', message: 'topology was destroyed' }
  insert
  
  { [MongoError: topology was destroyed] name: 'MongoError', message: 'topology was destroyed' }
  </code></pre>
  
  <p>The trouble is that from this on, when I restart mongod, the connection <strong>is not re-establised</strong>.</p>
  
  <h1>Solutions?</h1>
  
  <p>Does this problem have a solution? If so, do you know what it is?
  Once my app starts puking "topology was destroyed", the only way to get everything to work again is by restarting the whole app...</p>