我想知道这是否可能。假设我想查看集合中的所有文档,但想过滤掉那些将某个对象设置为GenericReferenceField()
的人答案 0 :(得分:1)
寻找对象'类型'是Mongoengine如何序列化文档的问题。因此,您始终可以使用原始查询进行检查。请考虑以下事项:
> use project1
switched to db project1
> db.page.findOne()
{
"_id" : ObjectId("52e862b11d41c80802167cc4"),
"title" : "Using MongoEngine",
"bookmark_object" : {
"_cls" : "Link",
"_ref" : DBRef("link", ObjectId("52e862b11d41c80802167cc3"))
},
"date_modified" : ISODate("2014-01-29T13:08:49.315Z")
}
该类只在_cls
字段中from mongoengine import *
import datetime
connect('project1')
class Page(Document):
title = StringField(max_length=200, required=True)
bookmark_object = GenericReferenceField()
date_modified = DateTimeField(default=datetime.datetime.now)
class Link(Document):
url = StringField()
#link = Link(url='http://mongodb.com/')
#link.save()
#page = Page(title='Using MongoEngine', bookmark_object=link)
#page.save()
pages = Page.objects(__raw__={ 'bookmark_object._cls': 'Link' })
for item in pages:
print item.bookmark_object