title: How to check if MongoDB connection is alive in Node.js
body: <p>I have an app with MongoDB as backend. When the app is started, I set up the connection and use it later on requests.</p>
  
  <p>But if in the mean time my db conncetion fails (ie. mongod crashes), how can I check that on the request time?</p>
  
  <p>To clarify a bit:</p>
  
  <ul>
  <li>currently I have an "api.js", which does <code>db.once('open', function../* setup */)</code> </li>
  <li>on request, I do <code>db.find(conditions, function(err, res) { if (err) ...else ...})</code>.</li>
  </ul>
  
  <p>What I want to do is check if the connection is alive before the <code>db.find()</code> clause. So if it is down, I can try to restart the db connection.</p>
  
  <p>P.S. I know, I should probably set some sort of a connection pool or similar instead of keeping the connection alive all the time, but right now it's set as it is.</p>