I was recently given this snippet of code that generates an arbitrary document in MongoDB. I've never encountered $$ in Mongo before and I'm curious about its significance.
All I know is that if I run the code with a single $, then the n no longer appears... Anyone have an explanation?
What is the significance of $$ in $$n
Code
db.collection.aggregate([ {"$limit": 1 }, { // use $range to generate iterator [1, 2, 3]"$addFields": {"rg": {"$range": [ 1, 4 ] }, globalVar: 0.001 } }, { // do the mapping according to logic"$addFields": {"cte": {"$map": {"input": "$rg","as": "n","in": { n: "$$n", f1: {"$multiply": ["$$n", 20 ] }, f2: {"$cond": {"if": { $lt: [ {"$multiply": ["$$n", 6 ] }, {"$multiply": ["$globalVar", 100 ] } ] },"then": {"$multiply": ["$$n", 6 ] },"else": {"$multiply": ["$globalVar", 100 ] } } } } } } } }, { // wrangle back to expected form"$unwind": "$cte" }, {"$replaceRoot": {"newRoot": "$cte" } }]);
The Mongo Playground