问题描述
例如..在Mongodb ..
For Example.. In Mongodb..
> db.test.findOne({}, {'mapField.FREE':1})
{
"_id" : ObjectId("4fb7b248c450190a2000006a"),
"mapField" : {
"BOXFLUX" : {
"a" : "f",
}
}
}
mapField字段由Mongoengine的MapField制成。
和'mapField'字段有一个密钥和数据的日志..但是我只是检索'BOXFLUX'..
The 'mapField' field is made of MapField of Mongoengine.and 'mapField' field has a log of key and data.. but I just retrieved only 'BOXFLUX'..
这个查询在MongoEngine中不起作用....
例如..
this query is not working in MongoEngine....for example..
BoxfluxDocument.objects( ~~ querying ~~ ).only('mapField.BOXFLUX')
AS你可以看到..
mapField.BOXFLUX')或仅限(mapField__BOXFLUX)不起作用。
它检索所有'mapField'数据,包括'BOXFLUX'一个。
AS you can see..only('mapField.BOXFLUX') or only only('mapField__BOXFLUX') does not work.it retrieves all 'mapField' data, including 'BOXFLUX' one..
如何仅检索MapField的一个字段? / strong>
How can I retrieve only a field of MapField???
推荐答案
我看到有一张机票:
适用于我是一个示例测试用例:
Works for me heres an example test case:
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)
这篇关于Mongoengine,只搜索一些MapField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!