What is the correct way to structure addresses in Mongodb?

Asked

Viewed 763 times

2

When I learned SQL, the way address was structured was to create a series of tables (neighborhoods, cities, states, countries). So I listed the tables. Then just add only the neighborhood id to the registration in question(customer, supplier, collaborator...) and the rest was on the basis of "Inner Join".
Now, I started using Mongodb. It is possible to get similar result with several collections, analogously to SQL in Mongodb, by running a series of callbacks in the find.

This is a recommended practice, or should I add all fields directly into the record, or is there some other better procedure?

  • Remove the Node.js tag from your question, your question is unrelated to this technology.

  • Why is there no connection with this technology? There can be something related to it that my practice is not recommended, such as server bottleneck, or something else. Ok I removed the tag.

1 answer

2

I believe there is no correct answer either in RDBMS (SQL) or Nosql. The design of the data will always depend on the purpose of your system.

If you are developing something that catalogues geographic entities (cities, streets, neighborhoods) like Google Maps or some delivery service, then it may make sense to ultra-normalize to the level of street and block.

On the other hand, if you’re just collecting customer addresses, and just need a single address, like Address of Collections, it would be much simpler to put the fields addressee, neighborhood, city, UF, parents in the client’s own table.

Similarly it is in Mongo. You can store collections of cities, neighborhoods, etc (ex: a neighborhood {_id:1, nome: 'Centro', cep: '12345-123', id_cidade: 123}) and make reference in another collection (ex: client {_id: 456, nome: 'Restaurante da Praça', endereco: {rua: 'Av Tancredo Neves, 333, sala 14', id_bairro: 1} }). Or you can just nest everything (e.g., customer {_id: 456, nome: 'Restaurante da Praça', endereco: {rua: 'Av Tancredo Neves, 333, sala 14', bairro: 'Centro', cidade: 'Patópolis', uf: 'XY', pais: 'Disney World', cep: '12345-123'} }).

In both cases (relational x nosql) there are advantages and disadvantages. It is up to you to analyze factors like: how and how often these data will be accessed and modified, impact on application performance, data maintenance difficulty, disk size, etc.

My advice tends to side with start simple, revisit, and Refine if you need to.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.