我的要求是先打开数据库,然后执行第二种方法。我试过我没有在JS中做过这样的事情,所以任何建议/指导都非常感谢。
我的代码可以在这里找到。如何使它异步?我可以选择设置超时,但我找不到好的做法......
任何示例都将非常有帮助。
这段代码来自这个 https://github.com/mongodb/node-mongodb-native 自述文件
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
var collection = db.collection('test_insert');
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
console.log(format("count = %s", count));
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
console.dir(results);
// Let's close the db
db.close();
});
});
})