How do I tell that directors is a array optional lab week 1

Helllo,

im trying to find a way to make a intersect between the directors and the cast like this:

{$project: {
 "sections": {$setIntersection:["directors", "cast"]}
}

but now I see a message that one of the elements need to be a array and not a string.

How to solve this one ?

When a field name is on the right side of : for an expression (like for setIntersection), the field name must be preceded by to indicate that the value is a field name. So

{$setIntersection:["$directors", "$cast"]}

should do the trick.

Examples are given at https://docs.mongodb.com/manual/reference/operator/aggregation/setIntersection/

I did that but I see then this :

db.movies.aggregate([{$project: {   writers: {   $map: {     input: "$writers",     as: "writer",     in: {       $arrayElemAt: [         {           $split: [ "$$writer", " (" ]         },         0       ]     }   } },  }}, {$project: {  "sections": {$setIntersection:["$directors", "$cast"]} }  }])
{ "_id" : ObjectId("573a1390f29313caabcd4192"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd414a"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd41a9"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd41aa"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4136"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4178"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd418c"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4135"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd41f0"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd413e"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd41b1"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd421c"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4217"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4256"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4218"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4274"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4137"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd42bf"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd4292"), "sections" : null }
{ "_id" : ObjectId("573a1390f29313caabcd428d"), "sections" : null }

so I thought I was wrong because sections is null

Hi @Roelof_Wobben,

In your first $project stage you are not projecting the directors and cast field, that is why you are getting null output.

Thanks,
Sonali