This is an example location from my collection.
{
"_id": { "$oid": "61fe9b6a390bd807cb3f8258" },
"name": "Spectrum Center",
"coords": {
"lat": { "$numberDouble": "35.225088" },
"long": { "$numberDouble": "-80.8397" }
},
"address": "333 E Trade St",
"country": "USA",
"city": "Charlotte",
"category": "concert",
"events": [
{
"name": "ANUEL AA: LEGENDS NEVER DIE WORLD TOUR",
"image": "",
"link": "",
"date": "2022-10-08",
"time": "20:00",
"expiresAt": { "$numberDouble": "1.6653456000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "624ef91f5030c078c684e572" }
},
{
"name": "Greta Van Fleet - Dreams In Gold Tour 2022",
"image": "",
"link": "",
"date": "2022-10-28",
"time": "19:00",
"expiresAt": { "$numberDouble": "1.6670700000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "62703fd9e9212167a21e153c" }
},
{
"name": "Daddy Yankee La Última Vuelta World Tour",
"image": "",
"link": "",
"date": "2022-12-06",
"time": "20:00",
"expiresAt": { "$numberDouble": "1.6704432000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "628ea28b50b698ff61909c13" }
},
{
"name": "Lizzo: The Special Tour",
"image": "",
"link": "",
"date": "2022-10-20",
"time": "20:00",
"expiresAt": { "$numberDouble": "1.6663824000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "628ea2c150b698ff61909c24" }
}
],
"__v": { "$numberInt": "0" },
"slug": "spectrum-center"
}
What I am trying to do is transforming it into:
{
"_id": { "$oid": "61fe9b6a390bd807cb3f8258" },
"name": "Spectrum Center",
"coords": {
"lat": { "$numberDouble": "35.225088" },
"long": { "$numberDouble": "-80.8397" }
},
"address": "333 E Trade St",
"country": "USA",
"city": "Charlotte",
"category": "concert",
"events": [
{
"name": "ANUEL AA: LEGENDS NEVER DIE WORLD TOUR",
"image": "",
"link": "",
"date": "2022-10-08",
"time": "20:00",
"slug": "anuel-aa-legends-never-die-world-tour",
"expiresAt": { "$numberDouble": "1.6653456000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "624ef91f5030c078c684e572" }
},
{
"name": "Greta Van Fleet - Dreams In Gold Tour 2022",
"image": "",
"link": "",
"date": "2022-10-28",
"time": "19:00",
"slug": "greta-van-fleet-dreams-in-gold-tour-2022",
"expiresAt": { "$numberDouble": "1.6670700000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "62703fd9e9212167a21e153c" }
},
{
"name": "Daddy Yankee La Última Vuelta World Tour",
"image": "",
"link": "",
"date": "2022-12-06",
"time": "20:00",
"slug": "daddy-yankee-la-ultima-vuelta-world-tour",
"expiresAt": { "$numberDouble": "1.6704432000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "628ea28b50b698ff61909c13" }
},
{
"name": "Lizzo: The Special Tour",
"image": "",
"link": "",
"date": "2022-10-20",
"time": "20:00",
"slug": "lizzo-the-special-tour",
"expiresAt": { "$numberDouble": "1.6663824000000E+12" },
"_location": { "$oid": "61fe9b6a390bd807cb3f8258" },
"_id": { "$oid": "628ea2c150b698ff61909c24" }
}
],
"__v": { "$numberInt": "0" },
"slug": "spectrum-center"
}
Notice how for every event there is a slug added, which is the name of the event transformed into slug.
I have a JS function that can transform the event name strings into the event slug strings, however I do not know how to access every event name separately, transform it with my function and add the output slug string inside the event object.
Here is also a screenshot of my db, collections, etc.:
So far I’ve tried following approaches:
locations.aggregate([
{
$unwind: "$events",
},
{
$addFields: {
"events.slug": {
$function: { body: string_to_slug, args: "$events.name", lang: "js" },
},
},
},
{ $out: "events_with_slugs" }
]);
Here the string_to_slug function is my JS function which transforms an event name string into event slug string. But this approach is not working. Or at least I do not know how to group it all.
The other thing that I’ve tried was to get all event names as an array and transform them into array of slugs … but I do not know how to insert them into the events afterwards:
locations.distinct("name", function (err, docs) {
docs.map(async (doc) => {
const location = await locations.findOne({ name: doc });
const location_events = location.events;
const names = location_events.map(({ name }) => {
return name;
});
const slugs = names.map((name) => {
return string_to_slug(name);
});
//-------------------------
//here I do not know how to insert the slugs into the respective event
});
});
I hope with this code snippets and the screenshot of my data it is bit clearer what I am trying to achieve.
Regards