This question already has an answer here:
- search by ObjectId in mongodb with pymongo 1 answer
I a list of ObjectId's that I'm iterating over to the find values in a dict where the keys are ObjectId's.
email_count = 0
# user_id_list is a list of ObjectId's
for user_id in user_id_list:
# UuserIdemailCountD is a dict where they keys are objectIds
email_count += UuserIdemailCountD[user_id]
I keep getting the following error:
email_count += UuserIdemailCountD[user_id]
KeyError: ObjectId('54a9c84ebf2e4e5b258b5412')
When i iterate over user_id_list
and just print the ids, I get a plain string like this 54a9c84ebf2e4e5b258b5412
.
Is the answer to convert the string to ObjectId's? If so, how?