Chapter 3 $lookup lab suggestions

I looked at the lab’s solution and I now understand where I went wrong.

I do have a couple suggestions though… If you are going to use the $regex expression, you really should work it into your aggregation pipeline instruction. It is an art form to learn how to use, but once you understand it, it simplifies it a lot.

Also… Why unwind the $lookuop array so long as I know I will have empty sets? It seems to be a waste of resources.

Also… Group and Sort are blocker stages in the aggregation pipeline. Why what is the advantage of using both when I have already limited my output to four possible answers? Therefore the Sort is redundant in my opinion.

Group yes… Sort no… in my opinion

1 Like

That’s the beauty in the pipeline, we have so many options. Have a look at the structure of my pipeline:

[
  {
    $lookup: {  ...  }
  },
  {
    $project:  {  ...  }
  },
  {
    $match:  {  ...  }
  },
  {
    $group:  {  ...  }
  }
]

Yea… There really is more then one way to skin a cat. I am reading Practical MongoDB Aggregations and it is really informative. I also would like to point out that $project doesn’t HAVE to be used if you want all the fields. I myself have been guilty of using $project but remember, if you need some field later in your aggregation and you have used a $project prior to the step, chances are the field isn’t in the pipeline. (look at $project as an EXCLUSION filter, if you don’t include it in the $project, the field isn’t passed to the next stage)

That being said… I have had to rethink how I use my $projects.

Also remember that the driver can move $match stages in the pipeline for quicker queries… But with excessive project statements it is not always possible to optimize the pipeline. (can’t move a match that comes after a project to before the project)

Another interesting tidbit that the manual clued me into is the fact that there are blocking stages in the pipeline. Some things can be done in parallel while others have to wait for the execution to reach a certain point before they can execute (hence called a blocking stage). Just things to consider when building pipelines.

1 Like