$comment

Definition

  • $comment
  • The $comment query operator associates a comment to anyexpression taking a query predicate.

Because comments propagate to the profile log, adding a commentcan make your profile data easier to interpret and trace.

The $comment operator has the form:

  1. db.collection.find( { <query>, $comment: <comment> } )

Behavior

You can use the $comment with any expression taking a querypredicate, such as the query predicate indb.collection.update() or in the $match stage ofthe aggregation pipeline.For an example, see Attach a Comment to an Aggregation Expression.

Examples

Attach a Comment to find

The following example adds a $comment to afind() operation :

  1. db.records.find(
  2. {
  3. x: { $mod: [ 2, 0 ] },
  4. $comment: "Find even values."
  5. }
  6. )

Attach a Comment to an Aggregation Expression

You can use the $comment with any expression taking a querypredicate.

The following examples uses the $comment operator in the$match stage to clarify the operation:

  1. db.records.aggregate( [
  2. { $match: { x: { $gt: 0 }, $comment: "Don't allow negative inputs." } },
  3. { $group : { _id: { $mod: [ "$x", 2 ] }, total: { $sum: "$x" } } }
  4. ] )

See also

$comment