Chapter 1 Optional Lab: page introducing $map seems to have a gotcha

Can you use $match and $map together? The page introducing $map (Optional Lab - Expressions with $project) begins by using the aggregation stage operator $match, then describes the special case where a simple match is insufficient. The solution is to use $map; however, combining $match and $map produced an error in Compass: “unknown operator: $map” (See below)

I was able to use it with $project. If match+map is possible, then maybe there is a gotcha? (i copied and pasted from the page). Or, it’s not possible, in which case please explain where it can be used.

Compass-map

NOTE: I just noticed I quoted everything in this screen shot. It started as a copy and paste and then I started quoting everything and arrived at that example. Thats how confusion begins. I’m making up my own syntax rules now (>_<)

To use $map you need to be inside an $expr.

Example:

// document
{ _id : 0 , array_field_name : [ 1 , 2 , 3 ]  }

// $match stage with $map
{ "$match" :
  { "$expr" :
    { "$eq" :
      [
        [ 2, 3, 4 ]  ,
        { "$map" :
          { "input" : "$array_field_name",
            "in" : { '$add':  [ '$$this', 1 ] }
          }
        }
      ]
    }
  }
}

Note that your logic will not make sense even if coded properly.

  1. writers is your array_field_name
  2. which you want to match
  3. to a mapped version of itself

For the sake of future students maybe some of this material should be added to the course materials?

Hi @xtian_simon,
Thanks for pointing that out, I will forward this feedback to our Curriculum Team, and we will surely consider your feedback while fixing the same.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer