Group an array using reduce,without unwind group

Hello : )

On the chapter Pipeline Optimization - Part 2,it says its good practice use array map,filter,reduce etc
instead of unwind group .

Is it possible to group an array using reduce(without unwind+group),in the general case?

For simple example (instead of numbers could be documents etc)
[1 2 3 1 2 10 2] to become {“1” [1 1] , “2” [2 2 2] ,“3” [“3”] , “10” [10]}

Also is it possible to reference a field,by constructing the reference inside the pipeline,
like concatStrings("$" “myfield”)?
If i could do the second,i could check if the member is key to the reduced map,and update it
but i cant construct the reference based on what i see in the reduce.

In functional languages this is so simple to do,but i don’t know how to do it in mongodb.

Thank you

Hi @Takis,

Great question! We have a great community forum where you can have an in-depth conversation about specific use cases and interesting problems that you get to solve using MongoDB. I would highly recommend posting your question there for a more involved discussion.

I would also recommend you to go through the following documentation link:


Thank you.
I asked there also,i posted a new question also there,that is the root of the problem i think.

If anyone knows can help :slight_smile: