如果使用.toArray()
,我知道您可以在回调中使用db.close()
,如下所示:
db.collection('grades').find(query).toArray(function(err, docs) {
if (err) throw err;
console.dir(docs);
db.close();
});
但是如果你需要遍历数组呢? 1)迭代光标来构造数组然后2)迭代数组来完成你的工作似乎是过分的。所以我认为使用cursor.forEach()
会很好。
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) {
if (err) throw err;
var cursor = db
.collection('data')
.find({})
.sort({State: 1, Temperature: -1});
var prevState = '';
var curr;
cursor.forEach(function(doc) {
if (doc.State === prevState) return;
db.collection('data').update(doc, { $set: { month_high: true}});
prevState = doc.State;
});
db.close();
});
然而,这给了我一个错误:
~/code/m101js $ node test.js
/Users/azerner/code/node_modules/mongodb/lib/utils.js:97
process.nextTick(function() { throw err; });
^
TypeError: undefined is not a function
at /Users/azerner/code/node_modules/mongodb/lib/cursor.js:527:15
at handleCallback (/Users/azerner/code/node_modules/mongodb/lib/utils.js:95:12)
at /Users/azerner/code/node_modules/mongodb/lib/cursor.js:493:22
at handleCallback (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:234:5)
at /Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:533:22
at queryCallback (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:167:20)
at Callbacks.flush (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:86:7)
at Server.destroy (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:694:41)
at Server.close (/Users/azerner/code/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/Users/azerner/code/node_modules/mongodb/lib/db.js:273:19)
~/code/m101js $ node test.js
/Users/azerner/code/node_modules/mongodb/lib/utils.js:97
process.nextTick(function() { throw err; });
^
TypeError: undefined is not a function
at /Users/azerner/code/node_modules/mongodb/lib/cursor.js:527:15
at handleCallback (/Users/azerner/code/node_modules/mongodb/lib/utils.js:95:12)
at /Users/azerner/code/node_modules/mongodb/lib/cursor.js:493:22
at handleCallback (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:234:5)
at /Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:533:22
at queryCallback (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:167:20)
at Callbacks.flush (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:86:7)
at Server.destroy (/Users/azerner/code/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:694:41)
at Server.close (/Users/azerner/code/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/Users/azerner/code/node_modules/mongodb/lib/db.js:273:19)
~/code/m101js $
似乎是所有库代码,我不确定如何调试它。但看起来问题出现在db.close()
。
db.close()
放在哪里?答案 0 :(得分:2)
问题是db.close()
被调用,然后我试图与数据库进行交互。连接关闭后,您无法与数据库进行交互。
我的代码在连接关闭后尝试与数据库交互的原因是因为.update()
是异步的。
使用async.each为我工作。 async.each
为您提供回调,以便在迭代完集合中的所有内容后运行。我在该回调中调用了db.close()
,这是我想要关闭与数据库的连接的时间。