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.
Hello @chistayaaa! How were you doing it prior? Did you have a list of document ID's you looped over and deleted consecutively?
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.
Hmm, I apologize, I think I'm misunderstanding your intiial question.
Could you explain what you're deleting specifically. The user / the positions?
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).
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,
});
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
thanks for the way, but it doesn't work 🤷♀️
Check this:
https://www.mongodb.com/docs/manual/reference/operator/update/pull/
Maybe it has to be
{
$pull: { answers: { $elemMatch: { roadmapItemId: id } } }
}
thank you, I'll read and try !
It could be that payload.update transforms the query and that you need the mongoose model
payload.collections.student.Model
instead
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 ...)
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.
Can payload run arbitrary mongo queries?
Payload exposes access to the underlying Model, so yes.
payload.collections['your-collection-slug'].Model
as far as I understand, After Operation has not been added yet and it cannot be used?
Correct, like I said, once it is merged you will be able to use it 👍
okay, thanks!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.