rajeshpanwar
rajeshpanwar

Reputation: 1233

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

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').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);
                            db.killOp(op.opid);


                        }

                    })
                }


            });

I want to kill long running operation but db.killOp is giving error : undefined is not a function.

How can I run killOp in node.js MongoDB native Driver ?

Upvotes: 2

Views: 1505

Answers (2)

Ron Wertlen
Ron Wertlen

Reputation: 832

From MongoDB 3.2 onwards, the accepted answer will no longer work, since the system collections are no longer exposed.

Instead you have a command hash for this operation. This works for me:

db.command({currentOp:1})
                .then( result => {
                    if( result && result.inprog ) {
                        result.inprog.forEach( item =>
                        {
                            if( // some condition 
                              ) {
                                  db.command( {killOp: 1, op: item.opid} );
                            }
                        });
                    }
                } )
                .catch( err => {
                    // don't forget to handle errors.
                       );  }  );

Upvotes: 2

chridam
chridam

Reputation: 103445

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

Upvotes: 0

Related Questions