I'm currently working on displaying the total number of employees for each state within companies located in the USA. I aim to showcase this information for all states included in the dataset using sample numbers as a reference:
AZ : 1234
CA : 30000
FL : 43439
Within my data collection, documents are structured as shown here:
https://i.sstatic.net/Bor2M.png
To begin, I used $match to filter out companies with an office.country_code of "USA". Now, my next step is to calculate the total number of employees per state and group them accordingly. The challenge lies in dealing with the nested offices array, especially when companies have multiple offices stored within it.
https://i.sstatic.net/xkKJr.png
One approach I'm considering is utilizing $group to display the distinct states. However, extracting the distinct list of states proves to be tricky due to the nested structure of offices.state_code within an array, and the presence of multiple array elements in certain cases.
I hope this explanation clarifies my question. Thank you.