【问题标题】:OperationError: Could not save document (LEFT_SUBFIELD only supports Object: ancestors.0 not: 7)OperationError:无法保存文档(LEFT_SUBFIELD 仅支持 Object:ancestors.0 而不是:7)
【发布时间】:2013-07-28 15:52:58
【问题描述】:

我在 MongoDB 中有一个组织数据库。我正在尝试使用mongoengine 将数据保存在该数据库中。我正在使用Djnago 服务器。当我创建对象时,它工作正常,但在编辑后它给出了一些错误。

class Organization(Document):
    username= StringField()
    ancestors = ListField(ReferenceField('Organization',dbref=False), default = list)
    parents = ListField(ReferenceField('Organization',dbref=False),default = list)
    descendants = ListField(ReferenceField('Organization',dbref=False), default = list)


obj1 = Organization(username = 'kousik')
obj1.save()
<Organization: Organization object> #obj1 created

obj2 = Organization(username = 'chowdhury',ancestors = [obj1],parents=[obj1])
obj2.save()
<Organization: Organization object> #obj2 created

obj3 = Organization(username = 'kchowdhury',ancestors = [obj1,obj2],parents=[obj2])
obj3.save()
<Organization: Organization object> #obj3 creaed

obj1.descendants = [obj2,obj3]
obj1.save()
<Organization: Organization object> #obj1 updated

obj2.descendants = [obj3]
obj2.save()

Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    obj2.save()
  File "C:\Python27\lib\site-packages\mongoengine\document.py", line 267, in save
    raise OperationError(message % unicode(err))
OperationError: Could not save document (LEFT_SUBFIELD only supports Object: ancestors.0 not: 7)

【问题讨论】:

    标签: python django mongodb mongoengine


    【解决方案1】:

    这是 mongoengine 错误。我为此创建了问题:https://github.com/MongoEngine/mongoengine/issues/422

    目前的解决方案:

    1. 在更新之前使用完整的重新加载文档(只是 reload 不起作用,因为留下参考):

      obj1 = Organization.objects(username=obj1.username).first()
      obj1.descendants = [obj2, obj3]
      obj1.save()
      
      
      obj2 = Organization.objects(username=obj2.username).first()
      obj2.descendants = [obj3]
      obj2.save()
      
    2. 改用原子更新save

    3. 使用to_dbref 引用:

      obj1 = Organization(username='kousik')
      print obj1.save()
      # <Organization: Organization object> #obj1 created
      
      obj2 = Organization(username='chowdhury', ancestors=[obj1.to_dbref()], parents=[obj1.to_dbref()])
      print obj2.save()
      # <Organization: Organization object> #obj2 created
      
      obj3 = Organization(username='kchowdhury', ancestors=[obj1.to_dbref(), obj2.to_dbref()], parents=[obj2.to_dbref()])
      print obj3.save()
      # <Organization: Organization object> #obj3 creaed
      
      obj1.descendants = [obj2.to_dbref(), obj3.to_dbref()]
      print obj1.save()
      # <Organization: Organization object> #obj1 updated
      
      obj2.descendants = [obj3.to_dbref()]
      print obj2.save()
      # <Organization: Organization object> #obj2 updated
      

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 2011-05-10
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多