Filter an array by the given ID

I have this code.

Instead of hardcoding the value done in red, i need it to match with the cityId and get the respective element.

If I hardcode the cityId, it returns me the array element but if I pass cityId, it return null. What is the fix?

@duard_N_A you solved a similar problem here. MongoDB: Filter an array by _id using aggregate - #5 by duard_N_A. Can you help?

@Pavel_Duchovny

You shared the aggregation with hard-coded value, that is, the one that work. We need to seed the one you tried with the variable cityId in order to see what you are doing wrong.

It is not clear which language you are using from the screenshot you supplied.

1 Like