vesii
vesii

Reputation: 3128

The difference between $ and $$ in MongoDB

I was wondering what is the difference between $ and $$ in MongoDB.

For example:

    '$sum': {
        '$map': {
            'input': '$data',
            'as': 'currentData',
            'in': { '$size': '$$currentData.d' }
        }
    }

What if I will use $ instead of $$ in $$currentData.d.

Upvotes: 22

Views: 9857

Answers (1)

Ashh
Ashh

Reputation: 46441

$ is referred to the root document fields where as $$ referred to the variable names.

{
  "$sum": {
    "$map": {
      "input": "$data",
      "as": "currentData",
      "in": { "$size": "$$currentData.d" }
    }
  }
}

Here '$data' is the document array field and the $$currentData is the variable taken in the as expression of $map aggregation.

Upvotes: 30

Related Questions