Labor of Love - Optional, Chapter 1 - Need help!

Let’s find how many movies in our movies collection are a “labor of love”, where the same person appears in cast, directors, and writers

I have done it in 3 parts:

  1. Projecting writer names in a new column, along with directors and cast for further processing
  2. project common_laborer as set intersection of the above 3 fields
  3. I filter out the results where the intersection result is null.

Why am I getting 0 as output?

db.movies.aggregate([
    {$project: {
        writ_names: {
            $map: {
                input: "$writers",
                as: "writer",
                in: {$arrayElemAt: [{$split: [ "$$writer", " (" ] },0]} }
            }, _id:0, directors:1, cast:1}},
            {$project: {
                common_laborers: {
                    $setIntersection: ["$writ_names", "$director", "$cast" ]
                }}},
            {$match: {labor_of_love: {common_laborers: {$elemMatch:{$exists: true}} } } }
])

labor_of_love is not a field in the input document, Remove it and it should work.
{"$match": { “common_laborers” : {"$elemMatch" : {"$exists" : true}} } }

1 Like

Thank you @Stennie for editing this.

1 Like

Did that worked?

I am not sure it will. You project directors but you do $setIntersection with $director.

Corrected that @steevej