通过本机驱动程序访问MongoDB中的集合



我已经建立了一个mongo数据库,并通过本机驱动程序成功连接,如下所示:

var mongo=require('mongodb').MongoClient;
var db;
mongo.connect('mongodb://path/to/db',function(err,db1){
if(err){
    console.log(err);
}else{
    console.log('mongo connection established');
    db=db1;
}

});

然后,我尝试更新预先存在的集合:

if(db){db.test.save({hello:'world'});}

我收到一个错误,说cannot call save of undefined.

尝试使用以下语法:

db.collection('test').save({hello:'world'}, callback); 

在 Node 中.js需要使用 collection 方法来访问集合(这与 MongoDB shell 不同)

最新更新