https://university.mongodb.com/mercury/M121/2020_August_18/chapter/Chapter_1_Basic_Aggregation_-_match_and_project/lesson/59d3906652c2b118b3968415/problem

For Lab - Computing field in Chapter 1

Why cant i match the size of array myfield in the match stage using the below command-
db.movies.aggregate([ { $project: { myfield:{ $split: ["$title", " "] } } }, {$match:{1:{$eq:{$size:myfield}}}} ]).itcount()

It gives me error -
ReferenceError: myfield is not defined :

Whereas if i create myfield as size of array itself in projection stage, i am getting correct ans. Command used-
db.movies.aggregate([ { $project: { myfield:{$size:{ $split: ["$title", " "] }} } }, {$match:{myfield:{$eq:1}}} ]).itcount()

most of the time when a field name is on the right side of :, you must use $ in front of the field name to indicate that you refer to a field name rather than z value.

Even using $myfield also gives same error

Hi @Nishant_Mehta,

I see that you have completed the lab successfully. :clap:

Please feel free to reach out if you have any further questions.

Thanks,
Sonali