Can we delete more than one position at once? (We can, but ...)

default discord avatar
chistayaaa
3 months ago
22

Can we delete more than one position at once?


When I try to delete two items, only one is deleted.



I tried to use Before/After in CollectionDeleteHook, but there is no difference.

  • default discord avatar
    notchr
    3 months ago

    Hello @chistayaaa! How were you doing it prior? Did you have a list of document ID's you looped over and deleted consecutively?

  • default discord avatar
    chistayaaa
    3 months ago

    Hello, may be I don't understand the question, but I tried to do like this, using loops, and had the same result.



    There is no differences, because when I console the ids, which come when deleting several items, they are displayed.


    The difficulty is that for some reason there is an update of only one.

    2023-06-06_17.11.56.png
  • default discord avatar
    notchr
    3 months ago

    Hmm, I apologize, I think I'm misunderstanding your intiial question.



    Could you explain what you're deleting specifically. The user / the positions?

  • default discord avatar
    chistayaaa
    3 months ago

    Okay, there is a collection with answers (I attached the screen), and these answers are stored in User (has a relationship). And when I want to delete few answers at once, only one is deleted in User


    Perhaps I'm not fully describing



    As far as I understand, hooks (afterDelete, beforeDelete) work under the hood like that -- when you select several elements at once for updating, the hook first updates one element (deletes), and then removes the second one from the old document (that is, where the first one still remains).

  • default discord avatar
    notchr
    3 months ago

    Wouldn't you want to get the ID's of each answer and delete them?



    Using deleteMany



    const result = await payload.delete({
      collection: "posts", // required
      where: {
        // required
        fieldName: { equals: "value" },
      },
      depth: 0,
      locale: "en",
      fallbackLocale: false,
      user: dummyUser,
      overrideAccess: false,
      showHiddenFields: true,
    });
  • default discord avatar
    thgh.
    3 months ago

    Payload calls the beforeDelete hook multiple times in parallel (I think), so there is a race condition: at first all students have answers x,y,z. Then you remove x & y. Your code is updating the answers to y,z and to x,z.



    Consider using a more powerful Mongo query like this:


    payload.update({
      ...,
      data: {
        $pull: { answers: { roadmapItemId: id } }
      }
    })

    No idea if this is actually possible

  • default discord avatar
    chistayaaa
    3 months ago

    thanks for the way, but it doesn't work 🤷‍♀️

  • default discord avatar
    thgh.
    3 months ago

    Check this:

    https://www.mongodb.com/docs/manual/reference/operator/update/pull/


    Maybe it has to be


     {
        $pull: { answers: { $elemMatch: { roadmapItemId: id } } }
      }
  • default discord avatar
    chistayaaa
    3 months ago

    thank you, I'll read and try !

  • default discord avatar
    thgh.
    3 months ago

    It could be that payload.update transforms the query and that you need the mongoose model

    payload.collections.student.Model

    instead

  • default discord avatar
    chistayaaa
    3 months ago

    may be it's a silly question, but:


     const result = await payload.delete({
            collection: "students", 
            where: {
              'answers.roadmapItemId': { equals: id },
            },
          });

    answers

    is a field with

    type: 'blocks'

    , and this Block has a field

    roadmapItemId

    (I use it to delete item in

    answers

    by id),


    and this way to get it

    'answers.roadmapItemId'

    doesn't work, so can I use nested fields at all?



    Can we delete more than one position at once? (We can, but ...)

  • default discord avatar
    jarrod69420
    3 months ago

    I think this (

    https://github.com/payloadcms/payload/pull/2697

    ) will help alleviate that pain. Once this is merged, you could use an afterOperation and update the students collection 1 time instead of many times.

  • default discord avatar
    thgh.
    3 months ago

    Can payload run arbitrary mongo queries?

  • default discord avatar
    jarrod69420
    3 months ago

    Payload exposes access to the underlying Model, so yes.

    payload.collections['your-collection-slug'].Model
  • default discord avatar
    chistayaaa
    3 months ago

    as far as I understand, After Operation has not been added yet and it cannot be used?

  • default discord avatar
    jarrod69420
    3 months ago

    Correct, like I said, once it is merged you will be able to use it 👍

  • default discord avatar
    chistayaaa
    3 months ago

    okay, thanks!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.