python - Iterate through a PyMongo collection -
i need iterate through collection , values keys same name.
[ { "_id": { "$oid": "59b3824b44e96c07dceba8de" }, "place": "yellow stone", "time": "2017-09-08", "user": "user@gmail.com", "user_go": "yes" }, { "_id": { "$oid": "59b4ea8644e96c37c43be33a" }, "place": "yosemite", "time": "2017-11-10", "user": "user@gmail.com", "user_go": "yes" }, { "_id": { "$oid": "59b4ea9144e96c37c43be344" }, "place": "devils tower", "time": "2017-09-10", "user": "user@gmail.com", "user_go": "yes" }, ]
i'm wondering how place names from collection based on username. i've tried doing similar this:
data = db.voting.find({'user' : 'user@gmail.com'}) data = dumps(data) parsed = json.loads(data) x in parsed: key,value in x.items(): print("values: {}".format(value))
as might guess values keys, how values keys want? i've tried place = parsed[0]['place']
returns first "place" value not values. i'm sure i'm missing obvious proving tricky wrap head around.
i reading through docs , found cursour.foreach can't find example of how use it.
x['place']
you're looking for.
for x in parsed: place = x['place'] ... # place
Comments
Post a Comment