MongoDB profiler output: What is the "command" operation?

The following operations are treated as command operations in MongoDB −

1. count
2. findAndModify
3. aggregate

Command operations appear in the MongoDB profiler output with the op field set to "command" and include additional details about the specific command executed.

Syntax

db.collection.count(query, options);
db.collection.findAndModify({query, update, options});
db.collection.aggregate(pipeline, options);

Sample Data

Let us create a collection with documents −

db.demo443.insertMany([
    {"Name": "Chris"},
    {"Name": "Bob"},
    {"Name": "David"}
]);
{
   "acknowledged": true,
   "insertedIds": [
      ObjectId("5e78d281bbc41e36cc3caeb9"),
      ObjectId("5e78d285bbc41e36cc3caeba"),
      ObjectId("5e78d288bbc41e36cc3caebb")
   ]
}

Display all documents from the collection −

db.demo443.find();
{"_id": ObjectId("5e78d281bbc41e36cc3caeb9"), "Name": "Chris"}
{"_id": ObjectId("5e78d285bbc41e36cc3caeba"), "Name": "Bob"}
{"_id": ObjectId("5e78d288bbc41e36cc3caebb"), "Name": "David"}

Example: Count Command Operation

The MongoDB count example is as follows −

db.demo443.count();
3

In the profiler output, this count operation would appear with "op": "command" and include the command details in the "command" field.

Conclusion

Command operations like count, findAndModify, and aggregate are logged in MongoDB profiler output with the operation type "command", making them distinct from basic CRUD operations.

Updated on: 2026-03-15T02:59:59+05:30

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements