Doubt in modeling Mongodb

Asked

Viewed 163 times

0

From what I studied, when modeling an application using Mongodb one should always think about the application. Thinking about a system that has Promotion and Gender, where a promotion has a Gender, I saw that it will be necessary a screen to register the promotions and a screen where I will present all the Generos and when clicking on one of the Promotions of that Gender. In the above example what is the best way to work? Would you do with Referencedocuments or Embeddeddocuments? In the case of Embeddeddocuments how would you look to bring the Generos?

Thank you very much in advance.

1 answer

2


Following your requirements you could have a promotion document, in which gender is an attribute. Something like this:

{
  "_id" : "ObjectId("507f191e810c19729de860ea")",
  "tituloPromocao" : "Promoção legal com vários premios",
  "dataInicial" : "2017-05-01",
  "dataFinal" : "2017-05-31",
  "genero" : "genero exemplo"
}

To search all genres in Promotions Center you can use the distinct, whereas the name of Collection is "promo":

db.promo.distinct("genero");

The parameter indicates which field you want to return the distinct values. It is interesting to add an index in this field, to optimize the search for gender and the distinct itself.

You can save the possible genres in another Collection, with only one document, for example:

{
   "generos" : ["Pizza","Calzone"]
}

So you can popular a selection on the interface with the possible options. And you can make another screen to keep the genres. That way you don’t have to do the distinct every time you go looking for the possible genres. All this considering that its genre is only a String.

Another option would be to consider gender as a subdocument, within promotion:

{
  "_id" : "ObjectId("507f191e810c19729de860ea")",
  "tituloPromocao" : "Promoção legal com vários premios",
  "dataInicial" : "2017-05-01",
  "dataFinal" : "2017-05-31",
  "genero" : { "id" : "pizza", "descricao" : "Gênero da pizza"}
}

And save the genre in another Collection:

{
   "id" : "pizza"
  ,"descricao" : "Gênero da pizza"
  ,"nroPromocoes" : 7
}

This approach would only make sense if you have more information to keep gender, I put the number of promotions there as an example. If this information is very important to the application, if you will access this number very often, then keep this value. So you don’t have to count the number of promotions every time. Take into account that this has a cost: if you change the gender description you need to change all promotions that use this genre. If you associate a promotion with a gender, you need to change the gender promotion counter.

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

  • I get it. And then I could have the Promotion registration screen in the Genero field instead of having a select field to avoid the risk of having the genres "Pizza" and "Pizza". It’s kind of around?

  • That’s right. If you have a set of fixed genres you can use a list in the code, or another Collection in which you save a document with the genres, e.g..: {"generos" : ["Pizza","Calzone"]}.

  • Thank you so much, you helped so much!

  • Just one more question. In case Gender has other information like "description" for example, I know I can have a "Gender object" within the Promotion with all your information. If I keep another collection to keep the genres as you suggested I’ll have a repeat of information. That inside Mongodb is not "wrong" from what I understand, that’s right?

  • It is not wrong, but it has a cost. If you are going to use the same gender for various promotions, you would have to repeat the information several times. If I were to update this genre I would have to update in all promotions that use it. I will update the answer by adding this alternative.

Browser other questions tagged

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