How does python mongo traverse efficiently

  python
for i in db.users.find():
 print i

The equivalent of requesting mongo once per cycle is extremely inefficient.
Can I just get the data and put it in memory like find_one?

For large-scale traversal, I think we can consider:

  • Using an EXHAUST-type cursor, mongo can return the query results batch by batch and stream the data before the client requests.

  • Specify batch_size in find. The default value seems to be 101 document or size exceeds 1M. You can set a larger batch_size to specify the number of RouteTrips, as shown inhttps://docs.mongodb.org/manual/core/cursors/#cursor-batches