I'm having a problem trying to insert an object inside a multiple nested array in my mongoose schema for mongoDB. I have the following structure:
{
contries: [{
name: 'String',
states: [{
name: 'String',
cities: [{
name: 'String',
regions: [{
name: 'String',
habitants: [{
name: 'String',
age: Number
},
{
name: 'String',
age: Number
}
]
}]
}]
}]
}]
}
1 - Imagine I want to insert habitants inside a specific region in a specific city and so on, how can I build the query?
2 - If a recieve this entire json object as a request, I need to check if any of the country, city, state or region exist, if none of them exist, I need to create and insert the habitants in the array.
3 - If I need to get only the habitants array from one specific region in a specific city, how can I use projection operator to filter this? (I want mongo to filter, not the application).
4(BONUS) - Habitants array only contain the object Id of a habitant object, just need to use populate?