How to use $comment operator in MongoDB

MongoDB is a NoSQL database and supports various operator classes. Apart from the operator classes in MongoDB, several standalone operators like $comment provide assistance in understanding the Mongo queries. The $comment operator can be used to add a comment to any query predicate in MongoDB to understand the purpose of that query. As its name directs, it acts as a comment in MongoDB just like anyone adds a comment in any other programming language.

Sometimes, in a shared working environment where several developers work on a single module simultaneously, this may result in misunderstanding the query. MongoDB provides support for $comment that adds a short description to queries and thus results in understanding the queries for new developers. Moreover, the $comment operator can also be used with all other operators in a MongoDB environment.

In this article, we will explain the usage of $comment operator in the MongoDB context:

How $comment Operator works in MongoDB

The $comment operator working mechanism is based on the following syntax:

({<query>})._addSpecial("$comment", "<comment>")



Any of the above syntaxes can be used to put comments in queries.

How to use the $comment operator in MongoDB

This section will provide an insight to add comments using the $comment operator in MongoDB. Moreover, the following database and collection names are used in this section to refer examples:

Database: linuxhint

Collection: tech_store

The tech-store collection contains the list of laptops that shows the status of the stock:

Following list of documents reside inside the “tech_store” collection of “linuxhint” database.

> db.tech_store.find().pretty()


Description automatically generated

Example1: Using $comment operator with comparison operators

The following command will help to get the status of the stock: for this, a comment is added to each document by using the command mentioned below:

> db.tech_store.find({Price: {$gt: "$2000"}})._addSpecial("$comment", "these are gaming machines").pretty()

Or you can add comments in the following way as well:

> db.tech_store.find({Price: {$gt: "$2000"}}).comment("these are gaming machines").pretty()

Example 2: Using $comment operator with logical operators

The following command helps to understand the use of $comment with logical operators. It is observed that only the query section is manipulated and the $comment application remains constant.

> db.tech_store.find({ $or: [{Price: { $lt: "$2000"}}, {Price: {$lte: "$1000"}}]})._addSpecial("$comment", "these are general use machines").pretty()

Additionally, you can execute the following command to add the same comment:

> db.tech_store.find({ $or: [{ Price: { $lt: "$2000" }}, {Price: {$lte: "$1000"}}]}).comment("these are general use machines").pretty()

Example 3: Using $comment operator with array operators

In this example, $comment will be used to add comments on array query operators in MongoDB.

The command mentioned below shows the usage of $comment with $size operator:

> db.tech_store.find({Make: {$size: 4}})._addSpecial("$comment", "Gaming but refurbished machines").pretty()

Moreover, you can add the same comment by following command:

> db.tech_store.find({Make: {$size: 4}}).comment("Gaming but refurbished machines").pretty()

It is observed from all the above examples that the use of $comment is the same with any operator and it does not bother what operator or command is being used.


Software’s are being developed in an agile-based environment where changes are inevitable and continuous changes in code is required. Commenting plays a vital role in such conditions to flag the purpose of code. Similarly, MongoDB also has a $comment operator that is used to add comments to queries. In this descriptive post, we have demonstrated the ways to apply the $comment operator in various conditions on MongoDB. After its detailed analysis, it is noticed that $comment has its own way of action and does not depend on any other operator of a query. And if you add a comment with the wrong syntax, the result will not be displayed.

About the author

Adnan Shabbir