cursor = db.user.find()
for document in cursor:
print(document)I want to store this entire data into a variable(result) as a dictionary.How do I do that?
Hi Dakshina,
A Python dictionary is indexed by keys, with the requirement that the keys are unique within one dictionary. Depending on your use case, you can utilise the ObjectId (converted to string) returned as keys for your result dictionary.
For example:
cursor = db.user.find()
result = dict()
for document in cursor:
result[str(document["_id"])] = document
For questions that are related more to the use of Python language itself, i.e. converting an iterator into a Python dictionary , you can also post on StackOverflow Python to reach wider audience.
Regards,
Wan.