以下代码正在“工作”,因为它在控制台中返回文档:
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();
});
};
>
connected!
{ _id: 50b01b31597f14213a00010f,
date: '2012-12-07',
espn_id: '400277990',
hid: '20',
aid: '2',
home: '76ers',
away: 'Celtics',
season: '2013',
during: 'regular',
scrape: null }
但是当我查看mongod控制台时,我看到每次刷新页面时,似乎打开了越来越多的连接而没有关闭它们。在这里你可以看到 5 次刷新后,我现在显然有 25 个打开的连接(你必须向右滚动才能看到数字):
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)
我究竟做错了什么?