S
S
Sergei R2019-01-06 10:50:29
JavaScript
Sergei R, 2019-01-06 10:50:29

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'],
          },
        ],
      },
    ],
  },
]

the model looks like this:
const schema = new mongoose.Schema({
  country: { type: String },
  region: {
    name: { type: String },
    path: { type: Array },
    city: {
      name: { type: String },
      path: { type: Array },
    },
  },
})

Am I understanding it correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question