I have a dataset in MongoDB that contains array data for each state, each of which has it’s own inner array of zip codes. Here is a simplified example of the structure:
[
{
data: {
name: 'Alabama',
zip_codes: [
{
id: 00000,
population: 10
},
{
id: 00001,
population: 20,
},
...
]
}
},
{
data: {
name: 'Alaska',
zip_codes: [
{
id: 10000,
population: 5,
},
...
]
}
}
...
]
My objective is to create a MongoDB view of just the zip code data. I want this (retaining the order from the original document):
zip_codes: [
{
id: 00000,
population: 10
},
{
id: 00001,
population: 20,
},
{
id: 10000,
population: 5,
},
...
]
I’m fairly new to MongoDB and am looking at the aggregation pipeline, particularly group and projection operations so far, but have not quite come across a way to do this. To all the Mongo experts out there, is there a pipeline I can build that will accomplish this?
I’ve tried $match and $project stages but couldn’t quite find something for this problem. I feel like this is solvable with Mongo but I’m too inexperienced. Any pointers in the right direction are greatly appreciated.
BoulderPika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.