使用Mongoose的节点批处理脚本:由app关闭的连接

时间:2014-10-10 05:48:54

标签: node.js mongodb mongoose

在这段代码上抓了我头脑的一大部分。 实际上我需要以批处理方式处理用户帐户的子文档。为了简洁起见,我只是放了应该工作的批次的一部分......但事实并非如此。

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();

所有建议都非常欢迎!
感谢

0 个答案:

没有答案