Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 727
vesii Avatar asked Apr 06 '19 13:04

vesii


1 Answers

$ 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.

like image 58
Ashh Avatar answered Oct 19 '22 02:10

Ashh