猫鼬是否允许并发多个数据库请求? [英] Does mongoose allow for multiple database requests concurrently?

查看:83
本文介绍了猫鼬是否允许并发多个数据库请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到Mongoose每次收集最多只能打开一个连接,并且没有更改此连接的选项.

I read that Mongoose will only open one connection at maximum per collection, and there's no option to change this.

这是否意味着缓慢的mongo查询将使所有后续查询都等待?

Does this mean that a slow mongo query will make all subsequent queries wait?

我知道node.js中的所有内容都是非阻塞的,但是我想知道慢速查询是否会延迟所有后续查询的执行.以及是否有办法改变这一点.

I know everything in node.js is non-blocking, but I'm wondering whether a slow query will delay the execution of all subsequent queries. And whether there is a way to change this.

推荐答案

如果使用mongoose.connect()的默认方法,则它仅使用一个连接.要解决此问题,您可以创建多个连接,然后将指向同一架构的模型绑定到该连接.

It does only use one connection, if you use the default method where you do mongoose.connect(). To get around this, you can create multiple connections, and then tie a model pointing to the same schema to that connection.

像这样:

var conn = mongoose.createConnection('mongodb://localhost/test');
var conn2 = mongoose.createConnection('mongodb://localhost/test');
var model1 = conn.model('Model', Schema);
var model2 = conn2.model('Model', Schema);
model1.find({long query}, function() {
   console.log("this will print out last");
});
model2.find({short query}, function() {
   console.log("this will print out first");
});

希望有帮助.

更新 嘿,确实有效.通过注释更新,可以使用createConnection创建连接池.它使您可以同时从同一模型执行多个查询:

Update Hey, that does work. Updating from the comments, you can create a connection pool using createConnection. It lets you do multiple queries from the same model concurrently:

var conn = mongoose.createConnection('mongodb://localhost/test', {server:{poolSize:2}});
var model = conn.model('Model', Schema);
model.find({long query}, function() {
   console.log("this will print out last");
});
model.find({short query}, function() {
   console.log("this will print out first");
});

更新2-2012年12月
这个答案现在可能有点过时了-我注意到我一直在继续投票,所以我想我会更新它.现在,用mongoose包装的mongodb本地驱动程序的默认连接池大小为5,因此您可能不需要在mongoose中显式指定它.

Update 2 -- Dec 2012
This answer may be slightly outdated now--I noticed I've been continuing to get upvotes, so I thought I would update it. The mongodb-native driver that mongoose wraps now has a default connection pool size of 5, so you probably don't need to explicitly specify it in mongoose.

这篇关于猫鼬是否允许并发多个数据库请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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