与 async.series 类似的东西(代码未经测试,仅供参考):
var async = require('async')
, MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
var collection, db
console.log("Connecting to " + host + ":" + port);
async.series([
// Connect to DB
function(callback) {
var connectionString = format("mongodb://%s:%s/node-mongo-examples?w=1", host, port)
MongoClient.connect(connectionString, function(err, ref) {
if (ref) db = ref
callback(err, ref)
})
},
// Drop DB
function(callback) {
db.dropDatabase(callback)
},
// Erase all records from the collection, if any
function(callback) {
collection = db.collection('test');
collection.remove({}, callback)
},
// Insert 3 records
function(callback) {
async.each(new Array(3), function(cb) {
collection.insert({'a':i}, {w:0}, cb);
}, callback)
},
//Check records count
function(callback) {
collection.count(function(err, count) {
if (err) return callback(err)
console.log("There are " + count + " records in the test collection. Here they are:");
callback(err, count)
})
},
//Indicate items
function(callback) {
collection.find({}, function(err, items) {
items.forEach(function(item) {
if(item == null) return
console.dir(item);
console.log("created at " + new Date(item._id.generationTime) + "\n")
})
callback(err, items)
})
},
function(callback) {
collection.drop(callback)
}
], function(error, results) {
// disconnect from DB
error && console.error(error)
db && db.close();
})
或者像这样(混合瀑布和系列方法,没有全局变量):
var async = require('async')
, MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
async.waterfall({
// Connect to DB
db: function(callback) {
var connectionString = format("mongodb://%s:%s/node-mongo-examples?w=1", host, port)
MongoClient.connect(connectionString, callback)
},
// Drop DB
collection: function(db, callback) {
db.dropDatabase(function(err, result) {
var collection = db.collection('test');
callback(err, collection)
})
},
collModifications: function(collection, callback) {
async.series([
// Erase all records from the collection, if any
function(callback) {
collection.remove({}, callback)
},
// Insert 3 records
function(callback) {
async.each(new Array(3), function(cb) {
collection.insert({'a':i}, {w:0}, cb);
}, callback)
},
//Check records count
function(callback) {
collection.count(function(err, count) {
if (err) return callback(err)
console.log("There are " + count + " records in the test collection. Here they are:");
callback(err, count)
})
},
//Indicate items
function(callback) {
collection.find({}, function(err, items) {
items.forEach(function(item) {
if(item == null) return
console.dir(item);
console.log("created at " + new Date(item._id.generationTime) + "\n")
})
callback(err, items)
})
},
function(callback) {
collection.drop(callback)
}
], callback)
}
}, function(error, results) {
// disconnect from DB
error && console.error(error)
results && results.db && results.db.close();
})