I am working with mongoDB 4.2 using python 3.7 and library mongoengine 0.18.2. So, I want to delete a document but I got lookup error.
ODM:
from mongoengine import Document
from mongoengine.fields import *
class Myparent(Document):
fieldfoo = IntField()
fieldbar = IntField()
class Mychild(Document):
fieldfoo = StringField()
myparent = ReferenceField('Myparent')
Now, when I want to delete a child:
item = Mychild.objects.get(id=123456)
item.delete()
I got this error:
site-packages/mongoengine/queryset/transform.py", line 60, in query
fields = _doc_cls._lookup_field(parts)
site-packages/mongoengine/base/document.py", line 1032, in _lookup_field
raise LookUpError('Cannot resolve field "%s"' % field_name)
mongoengine.errors.LookUpError: Cannot resolve field "myparent"
Any clue? Thanks
The id you are providing is not a valid id. Mongo supports 12 byte binary BSON type format which is definitely not 123456
. If your id is correct then you can do the following:
item=Mychild.objects(pk='some_id')
Whatever is returned in item will be a list but id is unique so you can write it as:
item=Mychild.objects(pk='some_id').first()
item.delete()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.