Shyam Arjarapu

@shyam.arjarapu

Mastering MongoDB - currentOp

“Dashboard in the pilot's cockpit” by Mitchel Boot on Unsplash

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.

The MongoDB currentOp command via db.adminCommand

An alternative wrapper function for the above adminCommand

The MongoDB currentOp command via a db.currentOp() wrapper function.

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 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” }

The MongoDB currentOp command to help find queries not using any index

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.

The MongoDB currentOp command to help you find database operations with number of yields greater than or equal to 100

Operations waiting for a lock

The following example returns information on all the write operations that are waiting for a lock.

The MongoDB currentOp command to help you find 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.

The MongoDB currentOp command to help you find 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.

A bash script to help you download, import and generate load on the sample restaurant database.

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 reiterate a very important point — “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 currentOp output (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.

A JavaScript function to execute currentOp in a while loop to capture operations over a period of time.

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”.

More by Shyam Arjarapu

Topics of interest

More Related Stories