【发布时间】:2013-04-17 13:22:16
【问题描述】:
我有一个包含此信息的数据库:
{"_id":1, "test":6,"foo":[{"mom":5,"dad":10},{"mom":7, "dad":12}]}
{"_id":2, "test":9,"foo":[{"mom":6,"dad":20},{"mom":7, "dad":15}]}
{"_id":3, "test":10, "foo":[{"mom":10,"dad":13},{"mom":2, "dad":19}]}
我在 mongo 中使用 mom=7 从 db 查询:
cursor = foo.find({"foo.mom":7},{"foo.$":1,"_id":0, "test":1})
for key in cursor:
print key
它打印我这个:
{"test":6,"foo":[{"mom":7, "dad":12}]}
{"test":9,"foo":[{"mom":7, "dad":15}]}
如果我使用
print key['test']
我只会得到“测试”的结果
所以,问题是:我怎样才能得到这样的结果:
{"test":6,"foo":[{"dad":12}]}
{"test":9,"foo":[{"dad":15}]}
我尝试使用
print key["foo.dad"]
但它只返回一个错误
【问题讨论】:
标签: python mongodb python-2.7 pymongo bottle