Mongoengine官方文档中的介绍
从官方文档可以看出MapField
和DictField
的区别仅限于MapField
的类型必须与特定字段类型相匹配。
一个例子带你看清真相
def test_only_with_mapfields(self):
class BlogPost(Document):
content = StringField()
author = MapField(field=StringField())
BlogPost.drop_collection()
post = BlogPost(content='Had a good coffee today...',
author={'name': "Ross", "age": "20"}).save()
obj = BlogPost.objects.only('author__name',).get()
self.assertEquals(obj.author['name'], "Ross")
self.assertEquals(obj.author.get("age", None), None)
代码来源:https://stackoverflow.com/questions/10679458/mongoengine-retriving-only-some-of-a-mapfield