Help needed for last quiz chapter 1

chapter one with last quiz need help in query, not able find accurate answer

Hey @naresh_17303

Are you talking about the optional Lab? Labour of Love?

Yes, was not able to use map properly.

Hey @naresh_17303

Alright lets walk through this a little bit to help get you on the right track. Let me first say that the pipeline to complete this is not all that complex. So If you find yourself doing a lot then reconsider.

In saying that the task is to query for the documents

where the same person appears in cast, directors, and writers

So, and this is all straight for the Lecture for this lab as I cannot give too much away
1.

There are times when we want to make sure that the field is an array, and that it is not empty. We can do this within $match

{ $match: { writers: { $elemMatch: { $exists: true } } }

However, the entries within writers presents another problem. A good amount of entries in writers look something like the following, where the writer is attributed with their specific contribution

"writers" : [ "Vincenzo Cerami (story)", "Roberto Benigni (story)" ]

This presents a problem, since comparing “Roberto Benigni” to “Roberto Benigni (story)” will definitely result in a difference.

So to fix this they have given you the $map stage here

writers: {
  $map: {
    input: "$writers",
    as: "writer",
    in: {
      $arrayElemAt: [
        {
          $split: [ "$$writer", " (" ]
        },
        0
      ]
    }
  }
}
  1. Now that we have all of the field “sanitized” you can look in the documents for an aggregation operator that could help.

Something to keep in mind is this any field computed in a $project stage is not available in that stage with $field.

If you still face some problems let me know.

2 Likes

thanks for helping to break it down natac13! That definitely helped me find my solution!

I just finished the assignment after struggling for a while. My solution ended up with 5 stages, though I’m sure it could be simplified. But one thing that became very important while working on this was defining my stages as variables in a script and then building the pipeline one stage at a time in order to view the results and see where the problems lie. In my case, it came down to a couple typos >.<

Also, keep in mind that $map is not an aggregation stage on its own, be sure to look at the documentation (especially the example :slight_smile: )

Good luck!

1 Like