0
class Parent(Document):
   list_items = ListField(EmbeddedDocumentField(Child))

class Child(EmbeddedDocument):
   name = StringField()

I have created a function to delete the embedded document from the ListField using the

pull - Atomic Operator

Please check below:

def deleteItem(parent_id,name):
  Parent.get(id=parent_id).update_one(pull__list_items__name=name)

My question is how am i sure that this atomic update was performed successfully?

How would i know if the list item actually exists in the parent object ?

Drise
  • 4,310
  • 5
  • 41
  • 66
rjcossa
  • 117
  • 4
  • 18

1 Answers1

0

The interim solution I found was to loop through the array of list items till I find the item that satisfies the condition and deleting it from the list:

def deleteItem(parent_id, name):
   parent = Parent.objects.get(id=parent_id)
   for item in list(parent.list_items):
      if item.name == name:
         parent.list_items.remove(item)
         parent.save()
         return true
   return false

But i would rather do it solely using the database if there is a better solution

rjcossa
  • 117
  • 4
  • 18