我正在MongoDB集合上执行这样的查询:
cursor = collection.find({"activityArray":{"$elemMatch":{"sport":0}}},{"activityArray.sport" : 1, "activityArray\|here is result object
.id":1, "endo" : 1}) |20166249
for result_object in cursor[0:1]: |here is result object
print "here is result object" |20166249 |here is result object
print result_object["endo"] |20166249
# print result_object["activityArray.sport"]
# print result_object["activityArray"]["sport"]
# print result_object["sport"]
每条注释掉的行都会给我一个关键错误。如何访问由PyMongo查询生成的文档中的数组内的文档返回的那些字段?
答案 0 :(得分:0)
由于activityArray
是一个数组,你需要在该列表上有另一个循环,如下所示:
cursor = collection.find({"activityArray": {"$elemMatch": {"sport":0 }}},{"activityArray.sport" : 1, "activityArray.id":1, "endo" : 1})
for result_object in cursor[0:1]:
print result_object["endo"])
for activity in result_object["activityArray"]:
print activity["sport"]