在这段代码上抓了我头脑的一大部分。 实际上我需要以批处理方式处理用户帐户的子文档。为了简洁起见,我只是放了应该工作的批次的一部分......但事实并非如此。
err: { [MongoError: Connection Closed By Application] name: 'MongoError' }
var mongoose = require('mongoose');
var dbURI = 'mongodb://localhost/testdb';
mongoose.connect(dbURI);
var UserSchema = new mongoose.Schema({ email: String, name: String });
var User = mongoose.model('User', UserSchema, 'users');
mongoose.connection.once('open', function() {
console.log("Connection opened, starting batch process."); /// this gets printed to console
User.find({},{},function(e,d){ // error is here somewhere & yes I got plenty of users in collection :)
if(e)console.log("err:",e);
console.log("doc:",d);
});
});
mongoose.connection.close();
所有建议都非常欢迎!
感谢