Mongoengine官方文檔中的介紹
從官方文檔可以看出MapField
和DictField
的區(qū)別僅限于MapField
的類型必須與特定字段類型相匹配鸽照。
一個(gè)例子帶你看清真相
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