Embedded Documents

We have four collections countries, states, cities, and airports
We used the embedded document way to modeling these data.
That’s how we modeling these data
The country document

{
  name: string,
  code: string,
  location: {
    type: 'Point',
    coordinates: [number, number]
  },
  currency: {
     symbol: string,
     name: string,
     code: string,
  },
  translation: [
     {
        language: string,
        name: string
     }
  ]
}

And this is the state document

{
  name: string,
  code: string,
  location: {
    type: 'Point',
    coordinates: [number, number]
  },
  country: {
    name: string,
    code: string,
    location: {
      type: 'Point',
      coordinates: [number, number]
    },
    currency: {
       symbol: string,
       name: string,
       code: string,
    }
  },
  translation: [
    {
       language: string,
       name: string,
       country: {
         name: string
       }
    }
  ]
}

And this is the city document

{
  name: string,
  code: string,
  location: {
    type: 'Point',
    coordinates: [number, number]
  },
  state: {
     name: string,
     code: string,
     location: {
       type: 'Point',
       coordinates: [number, number]
     },
     country: {
       name: string,
       code: string,
       location: {
         type: 'Point',
         coordinates: [number, number]
       },
       currency: {
          symbol: string,
          name: string,
          code: string,
       }
     }
  },
  translation: [
    {
       language: string,
       name: string,
       state: {
         name: string,
         country: {
           name: string
         } 
       }
    }
  ]
}

and so on for the airport document
So we used this modeling technique for improving the searching query for a document cause we will make a lot of read queries and too much little write queries.
What i want to ask, are we right about that? or we should use the reference instead of the embedded and if we used it, will it affect the performance of the read query bcause of the lookup which will happen to find the reference?

1 Like

Hi,

First, welcome!

for answering more productively you should first draw the general idea of the app like how do the queries should look like? (fetch only one entity at a time?) and how frequent are these queries?

Generally speaking, data that fetched together should be stored together, so the idea of embedding makes sense.