我的要求是首先打开数据库,然后执行第二个方法。我试过了,我还没有在JS中做过类似的事情,所以任何建议/指导都会受到极大的赞赏。
我的代码可以找到这里。我如何使这是异步的?我有setTimeout的选择,但我找不到一个好的练习.
任何例子都是非常有用的。
发布于 2013-10-30 12:20:20
这段代码来自于这个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();
});
});
})https://stackoverflow.com/questions/19681505
复制相似问题