M121-Chapter1-Optional Lab- Is it possible in only 3 stages?

I have been able to complete this lab after many foolish and desperate attempts. :sweat_smile: using 4 stages
My question is:
Could it be solved with just 3 stages?
Thanks.

Share what you got and we might be able to help improve it.

But it is against the rules to publish the solution

db.movies.aggregate([
    {
        $match: {
            cast: {
                $elemMatch: {
                    $exists: true,
                    $not: { $size: 0 }
                }
            },
            directors: {
                $elemMatch: {
                    $exists: true,
                    $not: { $size: 0 }
                }
            },
            writers: {
                $elemMatch: {
                    $exists: true,
                    $not: { $size: 0 }
                }
            }
        }
    },
    {
        $project: {
            cast: 1,
            directors: 1,
            writers: {
                $map: {
                    input: "$writers",
                    as: "writer",
                    in: { $arrayElemAt: [{ $split: ["$$writer", " ("] }, 0] }
                }
            }
        }
    },
    {
        $project: {
            "labor of love": { $setIntersection: ["$cast", "$directors", "$writers"] },
        }
    },
    {
        $match: {
            "labor of love": {
                $not: { $size: 0 }
            }
        }
    }
]).itcount()

I just want to know if there is a better way to fix it

It is.

[ Editorial Comment ]

But that rule exists to prevent slackers and cheaters from getting a completion certificate without doing the actual work. But good slackers and cheaters always succeed anyway to slack and cheat no matter what hurdle we put. But we recognize them at the end because they are better at slacking and cheating than doing real work.

[ End of Editorial Comment ]

Some tips for verifying if a field is a non empty array I prefer:

"cast.0" : { "$exists" : "true" }

Your 2 $project stages could be a single one by moving the $map of the first $project in place of the $writers of the second stage. But at the cost of so much complexity. (Untested but should work)

$project:
{
  "labor of love":
  {
    $setIntersection:
    [
      "$cast" ,
      "$directors",
      $map:
      {
        input: "$writers",
        as: "writer",
        in: { $arrayElemAt: [{ $split: ["$$writer", " ("] }, 0] }
      }
  } ,
}

Harder to read and to debug than your own version.

1 Like

Well it has worked, I just had to tweak a bit, (a closing bracket ( ] ) was missing and wrap map with braces ( { } ). But it can be done in three stages. :+1:

$project: {
    "labor of love":
    {
        $setIntersection: [
            "$cast",
            "$directors",
            {
                $map:
                {
                    input: "$writers",
                    as: "writer",
                    in: { $arrayElemAt: [{ $split: ["$$writer", " ("] }, 0] }
                }
            }
        ]
    }
}
1 Like

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