How can I build a variable like the favorites list in Compass?

And make it available for other stages.
Nearly everything in the title :wink:
Thank’s for all those amazing tutorials !

You could use https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/ to add the array as a new field that can be used in later stages.

Hi !
Thank’s answering. I tried $addField stage solution, it’s working to create the array, but it seems that it doesn’t propagate to next stage ($match). I was curious to learn a method if any… I duplicated the favorites list in any stage needing it.

The sole purpose of addFields is to propagate the new fields to the next stages.

Errors are often, typos in the name of field in the next stage or a project stage that does not project the added fields. If you look at the following image you will see the new field is still there.

Hi !
I tried very hard but doesn’t propagate to the next stage that is a $MATCH stage and “$favorites” is not recognized. That said I created several variables in $project and $addFields stage that propagating very well.
As I already passed that Lab Quizz, there is no rush to answer this. Should I close the topic ?

Try the $all, $in operators. If that doesn’t work, you may need to $unwind the array, or select the array with the $exists: true selector.

Hello !
As a conclusion of that topic. I would say that $in operator have a different behavior in stage than query.
So it can’t test 2 arrays as argument in stage mode, but need a string as first arg and an array as second…
That is ok, I have made my solution differently.
Thank’s for your suggestions.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.