Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Delete item from array Field

default discord avatar
LuisLopezlg2 years ago
1

Hi, is there a way to delete/update/push an item from an array with payload? the item is a relationship field


I couldn't find something like that in the docs.


I was reading other questions here in discord and i ended up doing the next:



I got the "guildModel" from payload and then I just worked with mongoose to remove a specific item from the array.



export const deleteBusReferencesFromGuild = async ({ guildId, busId, req }) => { const { payload } = req; const guildModel = payload.collections["guilds"].Model; const data = { buses: { busId } }; try { const res = await guildModel.updateOne( { _id: guildId }, //@ts-ignore { $pull: data }, ); return res; } catch (error) { console.log({ error }); } };
  • default discord avatar
    Jarrod2 years ago

    @LuisLopezlg currently, you need to push the entire array in its updated format 👍

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.