Wednesday, July 08, 2015

To View current running operations in MongoDB

Every database includes multiple currentoperations running in the server. In this blog, we shall discuss the process list in MongoDB.

                    MongoDB is used to display all the documents that contains information on in-progress operations for the database instance which can be done using db.currentop() operation.If you pass a query document to db.currentOp(), the output returns information only for the current operations that match the query.In this post we are going to see the various output that are return by db.currentop().

To view the current active queries in the database:
db.currentOp(
      {
        "active" : true
    }
    )


To view the queries that are running more than ‘x’ seconds in the database:
db.currentOp(
      {
        "active" : true,
        "secs_running" : { "$gt" : 10}
      }
    )

To view the queries that are waiting for a lock and not a read:
db.currentOp().inprog.forEach(
      function(d){
        if(d.waitingForLock && d.lockType != "read")
          printjson(d)
        })


To view all active read queries:
db.currentOp().inprog.forEach(
      function(d){
        if(d.active && d.lockType == "read")
          printjson(d)
        })


To view all active write queries:
db.currentOp().inprog.forEach(
      function(d){
        if(d.waitingForLock && d.lockType != "write")
           printjson(d)
        })


Kill the queries:
                              Incase if some of the queries that are running for more seconds and are degrading the database performance you can kill the using killOp() operation.The necessary parameter to kill is the opid(operation id) that will be returned by the currentOp().To kill a query with the opid 3258220 issue the below command.
db.killOp(3258220)

              So once done the query that has been mentioned will be killed which you can check again using the db.currentOp().



0 comments:

Post a Comment