. I found this article which refer to that problem. Nevertheless it is not clear to me which solution to take. Is it possible to use find().batch_size(30)? What exactly does the above command? Can I take all the database ids using batch_size?

You're getting this error because the cursor is timing out on the server (after 10 minutes of inactivity).

From the pymongo documentation:

Cursors in MongoDB can timeout on the server if they’ve been open for
a long time without any operations being performed on them. This can
lead to an CursorNotFound exception being raised when attempting to
iterate the cursor.

When you call the collection.find method it queries a collection and it returns a cursor to the documents. To get the documents you iterate the cursor. When you iterate over the cursor the driver is actually performing requests to the MongoDB server to fetch more data from the server. The amount of data returned in each request to fetch more data is set by the batch_size() method.