Answer the question
In order to leave comments, you need to log in
Do I understand correctly the tree structure of the collection in MongoDB with the storage of paths (Materialized Path)?
You need to create a collection structure to store data: { Country, Region, City } related to each other.
As I understand it, for this task, the best option would be a tree structure with path storage .
I put the structure in json
[
{
Country: 'country1',
Region: [
{
name: 'region1',
path: ['country1'],
city: [
{
name: 'city1',
path: ['country1', 'region1'],
},
{
name: 'city2',
path: ['country1', 'region1'],
},
],
},
{
name: 'region2',
path: ['country1'],
city: [
{
name: 'city3',
path: ['country1', 'region2'],
},
{
name: 'city4',
path: ['country1', 'region1'],
},
],
},
],
},
]
const schema = new mongoose.Schema({
country: { type: String },
region: {
name: { type: String },
path: { type: Array },
city: {
name: { type: String },
path: { type: Array },
},
},
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question