Nested Documents

Asked

Viewed 28 times

0

I have a question that I believe is basic for those who are starting with nosql and comes from the classic relational structures.

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

  • I have the table person with id and name_person;
  • I have the address table with id, id_pessoa, street and number;

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

Now let’s go to nosql. In the searches I did, the ideal is the address to be a sub-document person, 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 street number of "Vila do Chavez" without having an identifier?

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

1 answer

1


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

You answered the question by asking her, "... update the street number of 'Vila do Chavez' ..." you used the street field as 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 will search by the parent document first, I see no problem in identifying the sub-document by the field of the same patio. Remember that if you are changing this field you will need to keep the old value.

I copied this section below another reply mine, worth reading the whole to help in the concept.

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

  • Thank you. I thought this might be the answer. But it sounds strange, even for spending years working with relational banks. But I believe that this is characteristic of modeling.

Browser other questions tagged

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