$comment (misc. query)

APPLIES TO: MongoDB vCore

The $comment operator adds comments to queries to help identify them in logs and profiler output. This is particularly useful for debugging and monitoring database operations.

Syntax

The syntax for the $comment operator is as follows:

{ $comment: <string> }

Parameters

Description
string A string containing the comment to be included with the query.

Example

Let's understand the usage with sample data from the stores dataset.

db.stores.find(
   { "sales.totalSales": { $gt: 100000 } },
   { name: 1, "sales.totalSales": 1 }
).comment("Query to find high-performing stores")

This query finds stores with total sales greater than 100,000 and includes a comment for easy identification in logs. When executed against our sample dataset, it returns:

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "sales": {
    "totalSales": 151864
  }
}