MongoEngine没有看到预先存在的记录

时间:2012-02-03 21:02:38

标签: python mongoengine

我在MongoEngine中设置我的模型,并尝试查询它(我的集合中已有几十个文档),但查询集为空:

>>> mymodel.objects
[]

我可以添加新文档,这些文档会显示在后续查询中,但是有我之前存在的文档的标记:

>>> thingy = mymodel(a=2, b=3)
>>> thingy.save()
>>> mymodel.objects
[<mymodel: mymodel object>]

是什么给出的?作为参考,这是我的模型定义:

from django.conf import settings
from mongoengine import *

connect(settings.MONGO_DATABASE, 
        host=settings.MONGO_HOST, port=settings.MONGO_PORT)

class metadata(Document):

    meta = {'collection': 'metadata'}

    _id   = StringField()
    name  = StringField()
    _type = StringField()
    terms = ListField()
    level = StringField()
    abbreviation  = StringField()
    latest_update = DateTimeField()
    feature_flags = ListField()
    session_details  = DictField()
    legislature_name = StringField()
    lower_chamber_name = StringField()
    upper_chamber_term = IntField()
    upper_chamber_name = StringField()
    lower_chamber_term = IntField()
    lower_chamber_title = StringField()
    upper_chamber_title = StringField()
    _ignored_scraped_sessions = ListField()

如果MongoEngine本人的作者自己回答了这个问题,我希望事先表明你很棒!

1 个答案:

答案 0 :(得分:1)

没关系,文档回答了我的问题:

要将MongoEngine与现有数据一起使用,您必须在模型定义中设置allow_inheritance = False,因为MongoEngine的继承工具在文档上设置了其他属性(_cls和_types),然后(显然)使用它们来过滤MongoEngine查询。另一种选择可能是将这些属性添加到现有文档中。

http://docs.mongoengine.org/guide/defining-documents.html