Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
}
}
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB