Posts

Showing posts with the label db.killOp()

MongoDB Tips: Kill long running processes

This is just a quick and simple script to find all active opid that has been running over 10 seconds and they are running against the databases and collections you can specify in the "ns" filter. It's best to first run db.currentOp(    {      "active" : true,      "secs_running" : { "$gt" : 10 },      "ns" : {$in:["equipment.detachment","personnel.detail","personnel.unavailability","personnel.detachment"]}    } ).inprog.forEach(function (doc) { print(doc.opid); }) and verify your output before you execute the kill command in my experience to make sure you are killing the processes that are meant to be killed. Once verifed, simply run: db.currentOp(    {      "active" : true,      "secs_running" : { "$gt" : 10 },      "ns" : {$in:["<db1>.<col1>","<db2>.<col2>","<dbn>.<coln>...

MongoDB Tip: MongoDB Compass and kill long running transaction

Image
We can use MongoDB Compass to monitor database performance live, which is useful sometimes when we encounter some issues on the database where some queries are taking longer than normal.  In this case, we can kill these queries by first identifying the opid of the long running query using mongodb compass. This is the view of the MongoDB Compass tab,  click into the command on the top of the list of slowest operations Now copy the opid and use any client tool of your choosing, make sure you are connected to the primary node by first identify yourself with rs.isMaster() ismaster should contain the value "true" then  db.killOp(<opid>) Sometimes the transaction may take a few second to kill, and you click into the slowest operation, you can see the pendingKill for this opid is set to true.