Hi team, I have a setup that involves a many to many relationship. I am trying to provide a list of ids, for which i need to find each document, and add to the document the new id of the parent i am trying to create a new link for.
To elaborate, I have a list of menus, and a list of menu items. I want to provide a list of menu item ids, that will reference the menu it should belong to. However, the single menu does not only consist of ids. I have duplicated data to optimize the find attempts, and this duplicated data is a subset of the menu item. As a result, when i find and update the menu item to include its new single menu item parent, i want to get back a subset of all the menu item documents affected, to then provide to the menu.
How best can I accomplish this? For now this seems to work, but im wondering if it can be attained in a one shot query
const menuItems = await Promise.all(menuItemIds.map(async (menuItemId) => {
const { name, category, price, averageRating, images = {}, id } = await MenuItemModel.findByIdAndUpdate(menuItemId, {
$addToSet: { singleMenus: singleMenuId }
}, {
new: true,
runValidators: true
});
return {
name,
category,
price,
averageRating,
defaultImage: images.defaultImage,
id
};
}));