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.

Update, Insert and Delete from array

default discord avatar
d3_d3 years ago
2

Upon reading the docs for Local API I can see how to update a static field, or a field inside an object, but there isn't really anything about how to update fields in an Array.



Is there a link to where I can find documentation on this?


It would also be nice if we could have access to all the mongoose methods directly rather than the payload wrapped methods which is missing a lot of functionality from mongoose.



(There is 1 reference to it under Globals in Local API, but doesn't explain if we need to add the old data too or can just update a specific array element, or delete that specific one without also adding all the previous data that isn't being updated

https://payloadcms.com/docs/local-api/overview#globals

).



Also the ID references in links don't seem to scroll or snap to the targeted div id



found how to do it for those that find this



atm you have to add all the previous data for that field along with it to update, delete or insert the data

  • default discord avatar
    jessrynkar3 years ago

    hi

    @284901071468494848

    - glad you were able to find the answer! We definitely need to add an example for updating arrays. And you're right, something is amiss with our ID links in the docs. Will put both things on our radar, thanks for flagging 👍

  • default discord avatar
    rasmus14963 years ago

    I have a similar problem, except that it also involves l18n: Say I have a document which contains an array field, each element in the array contains a text field with localize=true. I have created a document with three elements in the array, each with an English string. Now I need to update the document to add Spanish strings to the three array elements. I would like to use the local API do to this. How would this be done?



    Figured it out: When doing the update, the id of nested fields need to be included along with their localized string values. So if I create the document like so:


        await payload.create({
            collection: 'questions',
            data: {
                id: 'q1',
                questionText: 'The question in English',
                answers: [
                    { id: 'a1', answerText: 'First answer in English' },
                    { id: 'a2', answerText: 'Second answer in English' },
                    { id: 'a3', answerText: 'Third answer in English' },
                ],
            },
        });
     

    then I update it with data in a different locale like so:


        await payload.update({
            collection: 'questions',
            locale: 'de',
            id: 'q1',
            data: {
                questionText: 'The question in German',
                answers: [
                    { id: 'a1', answerText: 'First answer in German' },
                    { id: 'a2', answerText: 'Second answer in German' },
                    { id: 'a3', answerText: 'Third answer in German' },
                ],
            },
        });
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.