Copy documents from one collection to another

You’re pulling everything down with the .toArray call which is bad for large collections.

If you’re just moving between collections in the same database or different on the same server then using the $out or $merge aggregation calls is much faster as it’ll all run server side.

If you were taking this approach (and I’ve done something similar for moving data from relational servers to Mongo) then you want to tune the batch size, 20 is tiny, unless each document is 16MB.

If you need to run with lots of data, then you’d want to get the iterator from the find call and loop through that, pushing an insertMany with the current batch as you go.