如何在执行回调后终止MongoClient.connect?

时间:2015-12-12 01:48:53

标签: javascript mongodb callback server

以下代码根据需要插入新文档{name:'r2'} 如何修改终止?

var MongoClient = require ('mongodb').MongoClient;

MongoClient.connect ('mongodb://localhost:27017/dbA', function (err, db) {

        if (err) {

            console.log (err);

        } else {

            var collection = db.collection ('colA');
            collection.insert ({name: 'r2'});

        } // end if (err)

});

1 个答案:

答案 0 :(得分:0)

要在执行插入后将其终止,我在if子句之后添加了行,以在插入完成后自动运行程序。

var MongoClient = require ('mongodb').MongoClient;

MongoClient.connect ('mongodb://localhost:27017/dbA', function (err, db) {

if (err) {

    console.log (err);

} else {

    var collection = db.collection ('colA');
    collection.insert ({name: 'r7'});

} // end if (err)

var exec = require ('child_process').exec;
var ppath = process.argv[1];

    // trim leading path up to the last '/'
var matched = ppath.match ('.*/(.*)');
var pName = matched [1];

//console.log ('pName: ' + pName);

var killCmd = 'kill -9 `ps -ef | grep ' + pName + ' | grep -v grep  | perl -lpe ' + "'" + 's/\\w+\\s+(\\d+).*/$1/' + "'" + '`';

//console.log ('killCmd: ' + killCmd);
exec (killCmd);

});