Chapter 1, as-variable only in scope in $map?

Working on the optional lab $map example in Mongo Compass.

In the example $map uses a variable $$writer in the as argument, and the document’s feature is called writers.

In Compass the $$writer variable was auto-complete available in the Compass editor, but error checking said it was undefined. (see below).

So I take it the $$writer variable is only in scope for the $map operator?

{
    $project: {
       writers: {
        $map: {
          input: "$writers",
          as: "writer",
          in: {
            $arrayElemAt: [
              {
                $split: ["$$writer", " ("]
              },
              0
            ]
          }
        }
      }
    }
  },

Yes, it is only available in the scope where it is defined. Since it is defined by the as: field of $map, it is only available in the in: of the same $map.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.