How to run db.killOp() using the MongoDB native Node.js driver?

前端 未结 2 1498
我在风中等你
我在风中等你 2021-01-22 08:16

I am using MongoDB native Node.js Driver 1.4.38.

I have got all running operation using :

var maxSecsRunning = 2;
db.collection(\'$cmd.sys.inprog\').fin         


        
2条回答
  •  生来不讨喜
    2021-01-22 08:26

    The same way you ran the query on the db.collection('$cmd.sys.inprog') collection, you can do the same for the db.killOp() on the db.collection('$cmd.sys.killop') collection.

    Following this example will do the trick:

    var maxSecsRunning = 2;
    db.collection('$cmd.sys.inprog').findOne(function (err, data) {
        if (err) throw err;
        if (data && data.inprog) {
            data.inprog.forEach(function (op) {
                console.log("Record", op);
                if (op.secs_running > maxSecsRunning && 
                    op.op == "query" && 
                    !op.ns.indexOf("local") > -1) {
                    console.log("Killing opId: " + op.opid 
                                                 + " running for over secs: " 
                                                 + op.secs_running);
                    // same thing as db.killOp(op.opid)
                    db.collection('$cmd.sys.killop')
                      .findOne({ 'op': op.opid }, function (err, data) {
                            if (err) throw err;
                            // do something with the result
                      });
                }
            });
        }
    });
    

提交回复
热议问题