title: How to close MongoDB connections properly in Node.js?
body: <p>The following code is "working", in that it returns the document in the console:</p>
  
  <pre><code>var Db = require('mongodb').Db;
  var mongoUri = 'mongodb://localhost:27017/basketball';
  
  exports.games = function(req, res){
      console.log(req);
      res.end("list of games");
      Db.connect(mongoUri, function(err, db) {
          console.log('connected!');
          db.collection('game_ids', function(err, coll) {
              coll.findOne({'date' : '2012-12-07'}, function(err, doc) {
                  console.log(doc);
              });
          });
          db.close();
      });
  };
  
  &gt;
  connected!
  { _id: 50b01b31597f14213a00010f,
    date: '2012-12-07',
    espn_id: '400277990',
    hid: '20',
    aid: '2',
    home: '76ers',
    away: 'Celtics',
    season: '2013',
    during: 'regular',
    scrape: null }
  </code></pre>
  
  <p>But when I look at the mongod console, I see that every time I refresh the page, it seems to open more and more connections without closing them. Here you can see after 5 refreshes, I now have 25 open connections apparently (you have to scroll to the right to see the numbers):</p>
  
  <pre><code>Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57587 #121 (21 connections now open)
  Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57588 #122 (22 connections now open)
  Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57589 #123 (23 connections now open)
  Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57590 #124 (24 connections now open)
  Sat Dec  8 12:29:32 [initandlisten] connection accepted from 127.0.0.1:57591 #125 (25 connections now open)
  </code></pre>
  
  <p>What am I doing wrong?</p>