How can I perform Group by query on Cosmos DB?

I have a Cosmos DB collection with 4 millions items. My main problem is how I can perform a ‘group by’ query on it.

For now I export it to json.gz blob storage. Convert it to a CSV file from a local C# program. Then run a simple R script on it that performs that group by.

I would like to run the whole process in Azure (using a Azure Logic App).

How can I do it and how can I do it in a efficient way?

Thank you!