我有一个包含以下信息的数据库:
{"_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}]}
我用 mom=7 从 db 查询 mongo:
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"]
但它只返回一个错误