无法使用mongoengine删除文档对象

问题描述 投票:0回答:1

我正在使用python 3.7和库mongoengine 0.18.2使用mongoDB 4.2。因此,我想删除一个文档,但出现查找错误。

ODM:

from mongoengine import Document
from mongoengine.fields import *

class Myparent(Document):
  fieldfoo = IntField()
  fieldbar = IntField()


class Mychild(Document):
  fieldfoo = StringField()
  myparent = ReferenceField('Myparent')

现在,当我想删除一个孩子:

  item = Mychild.objects.get(id=123456)
  item.delete()

我收到此错误:

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"

任何线索?谢谢

python mongodb mongoengine
1个回答
0
投票

您提供的ID无效。 Mongo支持12字节二进制BSON类型格式,这绝对不是123456。如果您的ID是正确的,则可以执行以下操作:

item=Mychild.objects(pk='some_id')

无论项目返回的内容是一个列表,但id是唯一的,因此您可以将其写为:

item=Mychild.objects(pk='some_id').first()

item.delete()

© www.soinside.com 2019 - 2024. All rights reserved.