Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Know which operations are currently running in the MongoDB server
Introduction
Have you ever been asked — ‘Why is your MongoDB database server running slow?’. If you did wonder, ‘Which operations are making it slow?’ then you are thinking in right direction. This is one of the many articles in multi-part series, Mastering MongoDB — One tip a day, solely created for you to master MongoDB by learning ‘one tip a day’.
In a few series of articles, I would like to give various tips to help you answer the above question. This article discusses currentOp command — its applications, use case scenarios and finally use case scenarios and finally some hands-on lab exercises.
Mastering — currentOp
What is currentOp
The currentOp is an administrative command that provides information about the current operations executing on the MongoDB server.
The currentOp must run against the admin database. When the authorization is turned on, the current user must either have ‘inprog’ privilege on admin database to view all operations or must use ‘$ownOps’ to view their own operations.
How to use currentOp
You could run the currentOp in mongo shell using either the adminCommand or the currentOp wrapper functions as show below.
An alternative wrapper function for the above adminCommand
Use cases for the currentOp
The use case for the currentOp command is to gathering information about operations currently executing on the MongoDB server. The the currentOp command helps you find
- Queries not using any index
- Operations with high numYields
- Operations waiting for a lock
- Long running operations
Queries not using any index
The below example will help you find query operations using no index { “planSummary”: “COLLSCAN” }
Operations with high numYields
The below example will help you find all operations with numYields greater than or 100 on guidebook database. Notice that the regular expression “ns”: /^guidebook./ is also used to further filter the operations for guidebook database.
Operations waiting for a lock
The following example returns information on all the write operations that are waiting for a lock.
Long running query
The following example returns information on all the operations running longer than 300 milliseconds.
Hands-On lab exercises
This lab exercise makes use of the restaurants sample dataset from MongoDB. Please follow the below instructions to import the restaurants data, generate some load to help you learn the concepts discussed in here.
Summary
The db.currentOp() command provides means to find all the queries that are currently running on your MongoDB server. The optional fields can be further leveraged to help filter the results to specific criteria.
I want to remind a very important point that “the db.currentOp() returns the information on in-progress operations”. If the queries of your interest execute very fast (a good thing), it may not even show up in currentOpoutput (most frustrating), if you ran the db.currentOp() command a tad bit early or late.
To ensure you capture all your operations over certain period of time, you may execute the db.currentOp() command in a while loop as shown below.
However, to capture operations over long period of time and consolidating the output for further processing would be become quite challenging. To help you properly capture all the queries over the period of time, you would need use Profiling. But that’s a topic for another day.
Hopefully, you learned something new today on you scale the path to “Mastering MongoDB — One tip a day”.
Mastering MongoDB - currentOp was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.