有没有办法通过一个mongodb连接来使用多个数据库?我发现了这个:
https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#open
但据我所知,这些文档已经很旧了,因为MongoClient上似乎没有open方法?你真的需要建立多个连接吗?
谢谢!
发布于 2015-06-01 12:48:15
找到了:
http://mongodb.github.io/node-mongodb-native/2.0/api/Db.html#db
下面是他们的例子
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
test.equal(null, err);
// Reference a different database sharing the same connections
// for the data transfer
var secondDb = db.db("integration_tests_2");
...它是同步的。对我来说似乎很奇怪,这个方法中没有"use“这个词。看起来也很奇怪,它属于db类。db.db('other_db')..有点晦涩难懂。做了一些测试,似乎起作用了,所以我会把这个标记为其他人的答案,在这里结束。
https://stackoverflow.com/questions/30559818
复制相似问题