Hi @JJ_Lee! Great question - this is a problem encountered by many Python developers!
A PyMongo find
call returns an iterable - which is something that can be looped over with a for loop. This has the advantage that the data downloads in chunks as you as you loop through the results - which means you don’t need to store the whole result in memory! This does mean that you can only loop through it once, as you’ve discovered.
The solution is to add all the results to a list
- which may use more memory, because all the results are now loaded into memory at one time - but it means you can loop through the list multiple times. Fortunately, this is quite straightforward in Python - you wrap the iterable with list
, like this:
coll1Datas = list(coll1.find({}))
coll2Datas = list(coll2.find({}))
I hope this helps! If you want to learn more about loops and iterables in Python, you should check out this talk by my friend Trey Hunner.