$comment

Adds a comment to a query without affecting the query execution, useful for documentation and debugging purposes.

Syntax

{ $comment: <comment_string> }

Parameters

comment_stringstringrequired

Comment text to attach to the query for documentation

Examples

Document query purpose

Add explanatory comment to describe query intent

Query:

db.products.find({ price: { $gt: 100 }, $comment: "Find expensive products over $100" })

Output:

Returns products with price > 100, comment appears in query logs for documentation

Debug complex aggregation

Add comments to complex aggregation stages for debugging

Query:

db.sales.aggregate([{ $match: { date: { $gte: new Date("2024-01-01") }, $comment: "Filter sales from 2024 onwards" } }, { $group: { _id: "$category", total: { $sum: "$amount" }, $comment: "Group by category and sum amounts" } }])

Output:

Aggregation with comments for each stage to aid in debugging and maintenance

Related