I have this connection set:
我有这个连接集:
var db = mongo.db('mongodb://localhost/inline_dev', {native_parser:true});
I then open a connection, and fetch some document...
然后我打开一个连接,并获取一些文件......
db.open(function(err, db)
db.collection('test').find().toArray(function(err, dbDocs) {
if (!err) {
console.log(dbDocs)
}
});
... but this seems to work as well, without the explicit .open()
:
...但这似乎也有效,没有明确的.open():
db.collection('test').find().toArray(function(err, dbDocs) {
if (!err) {
console.log(dbDocs)
}
});
Q) Why can/should I explicitly open()
the connection, given that it seems like the connection is opened as soon as I try to query a collection anyway?
问)为什么我应该/我应该明确地打开()连接,因为看起来连接是在我尝试查询集合时立即打开的?
Actually, it seems like the connection is opened as soon as I point my browser to the URL of the app (at least according to the mongod log).
实际上,一旦我将浏览器指向应用程序的URL(至少根据mongod日志),似乎连接就会打开。
1 个解决方案
#1
2
Mongoskin as much like other implementations do some "funny stuff" to cover up an initial connection which should actually be done in a callback. So really you should be waiting for the "open" to complete, but the actual process is "hidden" by holding the other operations until the connection is made.
像其他实现一样,Mongoskin做了一些“有趣的东西”来掩盖初始连接,这实际上应该在回调中完成。所以你应该等待“开放”完成,但实际过程是通过保持其他操作“隐藏”直到建立连接。
A good way to illustrate it to inspect the Object from each in code:
一个很好的方法来说明它从代码中检查每个对象:
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost/test");
console.log( "Before" );
console.log( db );
db.open(function(err,conn) {
console.log( "After" );
console.log( conn );
console.log( "And DB:" );
console.log( db );
});
But to your code in general it should not make a difference.
但是对于你的代码一般来说它不应该有所作为。
#1
2
Mongoskin as much like other implementations do some "funny stuff" to cover up an initial connection which should actually be done in a callback. So really you should be waiting for the "open" to complete, but the actual process is "hidden" by holding the other operations until the connection is made.
像其他实现一样,Mongoskin做了一些“有趣的东西”来掩盖初始连接,这实际上应该在回调中完成。所以你应该等待“开放”完成,但实际过程是通过保持其他操作“隐藏”直到建立连接。
A good way to illustrate it to inspect the Object from each in code:
一个很好的方法来说明它从代码中检查每个对象:
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost/test");
console.log( "Before" );
console.log( db );
db.open(function(err,conn) {
console.log( "After" );
console.log( conn );
console.log( "And DB:" );
console.log( db );
});
But to your code in general it should not make a difference.
但是对于你的代码一般来说它不应该有所作为。