如果使用mongoose.connect()的默认方法,则它仅使用一个连接。要解决此问题,您可以创建多个连接,然后将指向同一架构的模型绑定到该连接。
像这样:
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创建连接池。它使您可以同时从同一模型执行多个查询:
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月
现在这个答案可能有点过时了-
我注意到我一直在继续投票,所以我想我会更新它。猫鼬包装的mongodb本机驱动程序现在的默认连接池大小为5,因此您可能不需要在mongoose中显式指定它。



