【发布时间】:2012-10-21 12:44:44
【问题描述】:
方法 1 和方法 2 之间是否有任何区别(除了方法 1 是异步的)以获得对如下所示的 mongodb 集合的引用?
var mongo = require('mongodb');
var db= new mongo.Db('blog', new mongo.Server(host, port, {auto_reconnect: true}, {}));
db.open(function(){
//Method 1
db.collection('articles', function(err, result){
var collection1 = result;
});
//Method 2
var collection2 = new mongo.Collection(db, 'articles');
});
【问题讨论】:
-
你确定方法2是同步的吗? MongoDB 不应该创建一个集合,直到它以某种方式被触及,即添加数据或创建索引。如果它看起来是异步的,我认为那是 node.js 驱动程序本身,而不是 MongoDB。要么我认为它直接调用它的区别。