I am trying to query a list of documents from a collection using pymongo. The number of documents is usually above the 10k. After that I need to create a dictionary with the field id
as a key and this is the snippet I use to do it.
t1 = time.time()
mydocs = self.mypoint.find(myquery)
t2 = time.time()
print(f"[diagnostic] Time for query: {t2 - t1}")
res = dict()
count_points = 0
for mydoc in mydocs:
count_points += 1
res[mydoc["id"]] = mydoc
t3 = time.time()
print(f"[diagnostic] Time to reset dictionary: {t3-t2} - n_points = {count_points}")
The find works super fast while the for is extremely slow, so i wonder if there is a faster way to do that.
Thanks in advance, P