Optional Lab - Expressions with $project issue

Hey there,

I’m not quite sure why my answer is different from the given answer for this lab. I’m taking into account the duplicate documents that were mentioned and summing up the total labors of love by the condition that a name appears in all three fields (writers, directors, and cast) which is returning an incorrect 1682 value. Could you help me debug the pipeline? Thanks in advance!

    db.movies.aggregate([{
  $match: {
    cast: {
      $elemMatch: {
        $exists: true
      }
    },
    directors: {
      $elemMatch: {
        $exists: true
      }
    },
    writers: {
      $elemMatch: {
        $exists: true
      }
    }
  }
},
{
  $project: {
    "unique title": "$title",
    "cast": 1,
    "directors": 1,
    "_id": 0,
    "writers": {
      $map: {
        input: "$writers",
        as: "writer",
        in: {
          $arrayElemAt: [{
            $split: ["$$writer", " ("]
          },
            0
          ]
        }
      }
    }
  }
},
{
  $project: {
    "labors of love": {
      "$size": {
        "$setIntersection": ["$writers", "$directors", "$cast"]
      }
    }
  }
},
{
  $group: {
    _id: "$unique title",
    total: {
      $sum: "$labors of love"
    }
  }
},
{
  $group: {
    _id: null,
    'sum total': {
      $sum: "$total"
    }
  }
}
])

Hi @Isaac_Allen, welcome to the community.
It seems like you are not forwarding the unique title field to the $group stage. Since you have specified only one field(labors of love) in the later $project stage, only that field will be forwarded to the $group stage. Please use $addFields for these type of use cases.
Also, when I tried to reproduce your aggregation pipeline behavior it returned only 937 documents.
Ps: please read through the complete description given in the Detailed Answer to learn more.

I hope it helps.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hey @SourabhBagrecha thanks for the reply! So I went ahead and fixed the unique title routing break and am still getting the same behavior. I’ll add that it seems pretty strange that your reproduction returns 937 documents. Here’s a screenshot of my return value after said modification:

@SourabhBagrecha Here’s something interesting, I’m getting closer to the “correct” answer and I’ll tell you what I think is going on. I’m thinking the answer for this lab may not be taking multiple labor of love conditions for varying people involved in the same film into account. Here’s my code and output:

db.movies.aggregate([{

  $match: {

    cast: {

      $elemMatch: {

        $exists: true

      }

    },

    directors: {

      $elemMatch: {

        $exists: true

      }

    },

    writers: {

      $elemMatch: {

        $exists: true

      }

    }

  }

},

{

  $project: {

    "unique title": "$title",

    "cast": 1,

    "directors": 1,

    "_id": 0,

    "writers": {

      $map: {

        input: "$writers",

        as: "writer",

        in: {

          $arrayElemAt: [{

            $split: ["$$writer", " ("]

          },

            0

          ]

        }

      }

    }

  }

},

{

  $project: {

    "labors of love": {

      "$size": {

        "$setIntersection": ["$writers", "$directors", "$cast"]

      }

    },

    "unique title": 1

  }

},

{

  $group: {

    _id: "$unique title",

    total: {

      $sum: "$labors of love"

    }

  }

},

{

  $match: {

    total: {$gt: 0}

  }

},

{

  $count: "total"

}

])

@SourabhBagrecha Yep that’s what’s happening! I found a bug :smile: You guys should update the answer for this quiz to reflect mine! I love it when stuff like this happens cause it lets me know I’m really learning! Here’s my code and output for the code that is supposedly correct but not (technically).

db.movies.aggregate([{

  $match: {

    cast: {

      $elemMatch: {

        $exists: true

      }

    },

    directors: {

      $elemMatch: {

        $exists: true

      }

    },

    writers: {

      $elemMatch: {

        $exists: true

      }

    }

  }

},

{

  $project: {

    "cast": 1,

    "directors": 1,

    "_id": 0,

    "writers": {

      $map: {

        input: "$writers",

        as: "writer",

        in: {

          $arrayElemAt: [{

            $split: ["$$writer", " ("]

          },

            0

          ]

        }

      }

    }

  }

},

{

  $project: {

    "labors of love": {

      "$size": {

        "$setIntersection": ["$writers", "$directors", "$cast"]

      }

    }

  }

},

{

  $match: {

    "labors of love": {$gt: 0}

  }

},

{

  $count: "labors of love"

}

])

Hi @Isaac_Allen, thanks for reporting this.
I will forward this feedback to the appropriate channel and please be rest assured that our team will be making a fix as soon as possible.
Having said that, we are working on revamping this course and will soon be opening it up for beta testing, please let us know if you would like to be a part of that beta program.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer