Hi,
The problem is that the number of collections is dynamic
Could you provide a more concrete example of what you’re trying to do? Some document examples, workflow description, example results, and your MongoDB version would be useful.
When I try to map through each remaining collection (their names are stored in an array)
What is the significance of this array and where is it stored? Is it in a document somewhere, or is this internal to your application?
I initially aggregate the first two collections to a new collection, then call db.collection.aggregate on the newly created collection, so the data from the first two collections is stored.
If I understand correctly, you’re using $out multiple times to “collect” the $lookup results into one output collection. Is this correct? If yes, this will not work as expected since $out will overwrite an existing collection with the same name.
Best regards
Kevin