猫鼬是否允许并发多个数据库请求?

编程入门 行业动态 更新时间:2024-10-11 15:19:34
本文介绍了猫鼬是否允许并发多个数据库请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我读到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.

更多推荐

猫鼬是否允许并发多个数据库请求?

本文发布于:2023-11-25 06:43:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!