在Node.js上进行MongoDB连接的最佳实践是什么? [英] What's the best practice for MongoDB connections on Node.js?

查看:90
本文介绍了在Node.js上进行MongoDB连接的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说还不太清楚(我刚刚开始使用Node和Mongo),但由于服务器性能和压力(我想这是另一个问题,这确实使我感到担忧),但是我会到帖子末尾的内容.

This is something that is a bit unclear to me (I'm just getting started with Node and Mongo), and it really concerns me because of server performance and strain (which I guess is another question, but I'll get to that at the end of the post).

因此,假设我正在用Node.js和Restify编写一个API,其中每个API端点都对应一个函数,那么我应该:

So, assuming I'm writing an API with Node.js and Restify, where each API endpoint corresponds to a function, should I:

a)打开db连接并将其存储在全局变量中,然后仅在每个函数中使用它吗?
示例:

a) open the db connection and store it in a global var, and then just use that in every function?
Example:

// requires and so on leave me with a db var, assume {auto_reconnect: true}
function openDB() {
    db.open(function(err, db) {
        // skip err handling and so on
        return db;
    }
}

var myOpenDB = openDB(); // use myOpenDB in every other function I have

b)打开数据库连接,然后将所有内容都放入一个大的闭包中?
示例:

b) open the db connection and then just put everything in one giant closure?
Example:

// same as above
db.open(function(err, db) {
    // do everything else here, for example:
    server.get('/api/dosomething', function doSomething(req, res, next) { // (server is an instance of a Restify server)
        // use the db object here and so on
    });
}

c)每次需要时打开和关闭数据库吗?
示例:

c) open and close the db each time it is needed?
Example:

// again, same as above
server.get('/api/something', function doSomething(req, res, next) {
    db.open(function(err, db) {
        // do something
        db.close();
    });
});

server.post('/api/somethingelse', function doSomethingElse(req, res, next) {
    db.open(function(err, db) {
        // do something else
        db.close();
    });
});

这是我出于直觉而要做的事情,但是与此同时,我对此并不完全满意.难道对Mongo服务器造成的压力太大了吗?尤其是当(我希望我能做到这一点)时,它会收到数百个(如果不是数千个)这样的电话?

This last one is what I would do out of intuition, but at the same time I don't feel entirely comfortable doing this. Doesn't it put too much strain on the Mongo server? Especially when (and I hope I do get to that) it gets hundreds — if not thousands — of calls like this?

谢谢.

推荐答案

我非常喜欢 MongoJS .它使您可以以与默认命令行非常相似的方式使用Mongo,并且仅是官方Mongo驱动程序的包装.您只需打开数据库一次,然后指定要使用的集合.如果使用--harmony-proxies运行Node,甚至可以省略集合.

I like MongoJS a lot. It lets you use Mongo in a very similar way to the default command line and it's just a wrapper over the official Mongo driver. You only open the DB once and specify which collections you'll be using. You can even omit the collections if you run Node with --harmony-proxies.

var db = require('mongojs').connect('mydb', ['posts']);

server.get('/posts', function (req, res) {
  db.posts.find(function (err, posts) {
    res.send(JSON.stringify(posts));
  });
});

这篇关于在Node.js上进行MongoDB连接的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆