I'm using Reduce to create a joined String of fields from an array.
For example, let's say I have an array of subdocuments called children - and each child has a name field.
e.g
[
{name:"Zak"}, {name:"Bob"}, {name:"Sharon"}, {name:"Zak"}, {name:"Cindy"}, {name:"Bob"}, {name:"Peter"}
]
The expression below will give me a "csv" of "Zak, Bob, Sharon, Zak, Cindy, Bob, Peter, ";
(Yes - I know I can use $cond to detect the last iteration and remove the trailing ", ".
uniqueCsv: {
$reduce: {
input: "$children",
initialValue: '',
in: {
$concat: ["$$this.name", ", ", "$$value"]
}
}
}
Is it possible to dedupe and sort with this reduce so that we can have
"Bob, Cindy, Peter, Sharon, Zak" ?
Thanks!