Aggregating data in one query

Hello ,
I have to two doctypes

  • Product

Product Name
Price
Category

  • Category

Category Name

What i’m trying to do is create a query that gets all the categories with their products without the need to loop through the categories then getting the products. for example:

[
	{
		"category_name": "Mobile Phones",
		"products": [
			{
				"product_name": "Galaxy S22",
				"price": 999.99
			},
			{
				"product_name": "iPhone 13",
				"price": 1299.99
			}
		]
	},
	{
		"category_name": "Consoles",
		"products": [
			{
				"product_name": "Playstation 5",
				"price": 499.99
			},
			{
				"product_name": "Xbox Series X",
				"price": 599.99
			}
		]
	}
]

Any suggestions how do i go on that ? or any code snippets from the core or the other apps that maybe helpful.
Thank you