M121: Frequently Asked Questions on The MongoDB Aggregation Framework course

1. How can I connect to the cluster that is used for this course?

The cluster used for this course is different from the Sample Dataset you may have used in other courses like M001, please feel to use the following cluster connection string to start using the appropriate databases and collections used in this course:

mongosh "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --tls -u m121 -p aggregations

2. How to load the validateLab1.js file? The command returns an error: “file not found”.

This error is caused probably because you are running the validateLab1.js file from the wrong directory. Follow the steps mentioned below:

  1. Navigate to the correct folder by changing the directory using cd:

cd /path/to/validateFile

  1. Run mongosh in the same directory by executing:

mongosh “mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0” --authenticationDatabase admin --tls -u m121 -p aggregations

  1. Run the load command here using:


3. How to make Long Aggregation Pipelines look intuitive and readable?

There are 2 simple ways in which you can write your aggregation pipeline:

  1. Compass Aggregation Builder: Mongo Compass provides a really neat GUI-based aggregation pipeline builder that supports all kinds of aggregation stages and you can also export your pipelines to your favorite choice of programming language.
  2. The mongosh console is line-oriented. However, you can also use an editor to work with multiline functions.

There are two options:

  • An external editor, accessed with the edit command
  • A built-in editor, accessed with the .editor command ← (I guess what you are looking for is this)

4. In chapter 3: $lookup lab, how to filter all the airlines that contain 747 or 380 in their name?

To filter out all the airlines that contain “747” or “380” in their name, while there are many ways to do it, the simplest one is to use regex(Regular Expression), use the following $match stage:

   $match: {
      airplane: /747|380/

5. What is the benefit of $addFields over $project?

Basically, $project is verbose and inflexible.

If you want to define one new field or revise one field, you will have to name all other fields in the projection to include. If each input record has 100 fields and the pipeline needs to employ a $project stage for the first time, things become tiresome. To include a new 101st field, you now also have to name all the original 100 fields in this new $project stage too. You will find this irritating if you have an evolving data model, where additional new fields appear in some records over time. Because you use a $project for inclusion, then each time a new field appears in the data set, you must go back to the old aggregation pipeline to modify it to name the new field explicitly for inclusion in the results. This is the antithesis of flexibility and agility.

To learn more, please take a look at Better Alternatives To A Project Stage 2.

6. How to utilize $setIntersection operator for the Optional Lab in Chapter 1?

$setIntersection takes two or more arrays and returns an array that contains the elements that appear in every input array.

$setIntersection has the following syntax:

{ $setIntersection: [ <array1>, <array2>, ... ] } 

Let’s take a look at this example :

Consider an experiments collection with the following documents:

{ "_id" : 1, "A" : [ "red", "blue" ], "B" : [ "red", "blue" ] }
{ "_id" : 2, "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ] }
{ "_id" : 3, "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ] }
{ "_id" : 4, "A" : [ "red", "blue" ], "B" : [ "green", "red" ] }

The following operation uses the $setIntersection operator to return an array of elements common to both the A array and the B array:

   { $project: { A: 1, B: 1, commonToBoth: { $setIntersection: [ "$A", "$B" ] }, _id: 0 } }

The operation returns the following results:

{ "A" : [ "red", "blue" ], "B" : [ "red", "blue" ], "commonToBoth" : [ "blue", "red" ] }
{ "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ], "commonToBoth" : [ "blue", "red" ] }
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ], "commonToBoth" : [ "blue", "red" ] }
{ "A" : [ "red", "blue" ], "B" : [ "green", "red" ], "commonToBoth" : [ "red" ] }

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer