Matching field with another field in the same document

Hi Guys,

I have learnt from the lectures in the course that we can use $ to refer to a field in the document, but the $directors in my following $project is not defined. Please advise.

db.movies.aggregate([
{
$match: {
cast: { $elemMatch: { $exists: true } },
writers: { $elemMatch: { $exists: true } },
directors: { $elemMatch: { $exists: true } },
},
},
{
$project: {
_id: 0,
writers: {
$map: {
input: “$writers”,
in: {
$$this: { $in: $directors } <— not defined
},
},
},
},
},
]);

I have usually seen a query format like “field: {$in: ‘user-provided-array’}”". But I have not seen “field: {$in: ‘another-field-array’}”. Please advise the syntax to match a field with another field within the document.

Thanks much in advance for your help.

I just found the answer for my question. The $directors should have been quoted.

Glad you got it!

José Carlos