MongoDB How do we get all the open CURRENT cursors and the queries they run?

Some users open cursors for mongo with a set of noTimeout. I want to list all open cursors, their identifiers, their parameters, the query in which they are run, the start time for this cursor, the last time it was used (getmore). Is there a team for this? Can someone please give me any hints so that I can write a small application if necessary.

+7
source share
3 answers

MongoDB has a currentOp command that contains current operations (in general, not just open cursors). The result is an array of "inprog" values. You have a workflow identifier, not a cursor identifier, but it is very useful to investigate heavy operations or those that work for a long time. This is not exactly what you want, but I think you could write a small program that analyzes the execution time of operations to determine which one has been running for a certain period of time.

Look at the example of my aggregation database, which I run only for testing purposes, I will hide some data, because it is very reasonable in our case :)

"inprog" : [ { "opid" : 74074645, "active" : true, "secs_running" : 2, "op" : "query", "ns" : "mydb.Terms.ByHour", "query" : { "aggregate" : "Terms.ByHour", "pipeline" : [ { "$match" : { "cluster" : "my_key", "start" : { "$gte" : ISODate("2013-11-10T00:00:00Z"), "$lte" : ISODate("2013-11-11T00:00:00Z") } } }, { "$group" : { "_id" : "$start", "count" : { "$sum" : "$count" } } }, { "$sort" : { "_id" : 1 } } ] 

Just put the result in a variable:

 currentOps = db.currentOp() 

and use it like regular json, or write a small program that iterates over the currentOps.inprog array and checks for secs_running> something

Hope this helps.

+7
source

According to the comments on their JIRA in January 2017, this function is planned to be done, but it is not currently included in any roadmap: https://jira.mongodb.org/browse/SERVER-3090

+2
source

For people using mongoDB 4.2 and above, the db.runCommand("listCursors")

0
source

All Articles