我正在尝试连接到mongoDB数据库,并在集合上创建一些进程,并在处理完所有集合项时关闭集合。当我试图接收array.length时,我得到了未定义。
Db = require('mongodb').Db;
Server = require('mongodb').Server;
const db = new Db(DB_NAME, new Server(HOST, PORT));
// connect to mongoDB
db.open(function (err, db) {
const Collection = db.collection(COLLECTION_NAME);
var items = Collection.find({});
var itemsLength = items.lebgth;
var itemsProcessed = 0;
items.forEach((item, index, array) => {
// some process like:
Collection.update({query}, {set}, callback)
itemsProcessed++;
if(itemsProcessed == array.length){
db.close();
// close connection if all items were processed
}
});
});
还有其他办法吗?