We\'re trying to \'join\' an array of strings to a single string within an aggregation.
Given is the following dataset:
Collection 1:
{
id: 123
Sometimes it's easiest to use JavaScript:
db.getCollection('Collection1').aggregate([
{
$lookup:
{
from: 'Collection2',
localField: 'id',
foreignField: 'collection1_id',
as: 'col2'
}
}]).map((e) => ({
id: e.id,
field: e.field,
collection2: Array.isArray(e.col2) &&
e.col2.reduce((arr, el) => {
arr.push(el.name);
return arr;
}, []).join(', ')
}))