Nested documents

0

Personal I have a question that I believe is basic to anyone who is starting with nosql and comes from classic relational structures.

Well, let's look at a simple example for relational structures:

  • I have the table person with id and person_name;
  • I have the address table with id, people_id, street and number;

In the above description I have a one to many basic relationship where a person has many addresses. In a standard CRUD I can update an address of the person with the id of the address. If I want to list all the addresses of a person is also simple, simply filtering the address table by the person_id.

Now let's go to nosql. In the searches I've done, the ideal is the address being a sub document of the person document, which would look something like this:

"Pessoa":{
  "_id": "12asdf213",
  "nome": "João da Silva",
  "endereços": [{
    "logradouro": "Vila do Chavez",
    "numero": 71
  }, {
    "logradouro": "Cidade Z"
    "numero": 14
  }]
}

My question is, how could I update the number of the Vila Chavez area without having an identifier?

In searches I also found that I can reference by creating a document for the address and making a common relationship between the two documents. But that for nosql is not very performatic or indicated.

    
asked by anonymous 07.08.2017 / 14:22

1 answer

1
  

How could I update the number of the "Vila do Chavez"   without having an identifier?

You answered the question by asking her, "... update the Vila do Chavez's street number ..." you used the public domain field as an identifier.

Being objective : You need an identifier in the subdocument to know which one to update. You can create an ID, or search by name, as you go through the parent document first, I see no problem in identifying the sub-document by the same field of the street. Remember that if you are changing this field you will need to save the old value.

I copied this snippet below another answer mine, it's worth reading the whole thing to help with the concept.

  

The general guideline on modeling with MongoDB is: always structure   your data thinking about how you will access / modify / insert / delete   information.

    
10.08.2017 / 14:12