2011-12-12 201 views
0

我有mongodb(mongoose)的nodejs应用程序。像这样:nodejs。关闭连接

var mongoose = require('mongoose'); 
var express = require('express'); 
mongoose.connect('mongodb://localhost/my_database'); 

..... 

var DocumentSchema = new Schema({ 
    title : String 
    , body : String 
    , date : Date 
}); 

var Document = mongoose.model('Document', DocumentSchema); 

app.get('/documents', function(req, res) { 
    // get all docs 
    Document.find({}, function(err, docs) { 

     docs = docs.map(function(d) { 
      return {title: d.title, id: d._id}; 
     }); 

     res.render('documents/index.jade', {documents: docs}); 
    }); 
}); 

so所有用户使用单个MongoDB连接。我应该在每次请求后关闭连接吗?

回答

1

不,你不应该关闭连接并重新打开它,除非你想让你的进程消耗更多的cpu/RAM,否则它应该保持打开状态。