繁体   English   中英

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

[英]How to terminate MongoClient.connect after executing its callback?

以下代码根据需要插入一个新文档{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)

});

为了在执行插入操作后杀死它,我在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);

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM