How not to duplicate data in MongoDB (with NodeJS), migrating from MySQL


I am evaluating the possibility to start working with MongoDB using NodeJS, however, I am formatted to think in SQL and not in noSQL, I find myself with the following dilemma which takes a day chewing and all post / tutorials They seem to ignore this part so: either I do not understand or mongoDB does not apply to my case.

I have the following example case (only for the purposes of this question):

If I need to save a sale in MySQL, I would do it in tables:

Cabecera de la venta
1        |  001   | 20         | 200

Detalle de la venta
1        |  5          | 15              | 4        | 60
1        |  78         | 14              | 10       | 140

5           |  CAMISA  |
78          |  ZAPATOS |

20          |  JUAN PEREZ   |
150         |  ALBERTO RUIZ |

The same thing in MondoDB would be a single document:

    "NUMERO" : "1",
    "TOTAL" : "200",
    "DETALLE" : [
          {"PRODUCTO" : "CAMISA", "PRECIO_UNITARIO":"15", "CANTIDAD":"4"},
          {"PRODUCTO" : "ZAPATOS", "PRECIO_UNITARIO":"14", "CANTIDAD":"10"},


Now, we could have documents for each client / products, but they would only be in the form of a dictionary, since they would not be directly related to the detail of the sale.

1.Para los Productos
    {"PRODUCTO" : "CAMISA"},
2.Para los Clientes

And we omitted IDs, with which I have the following doubts:

  • This model, is it well planned?
  • AND MOST IMPORTANT: Imagine the real case in which the person who loads the products charged "SAPATOS" instead of "shoes", and sold for 4 days and nobody noticed the error, in which case all the sales documents entered with the error. In a relational scheme, I only change the PRODUCT table, and when doing the multiple INNER JOIN there would be no problem.
  • Or any other case of report, imagine if I want to obtain the sales grouped by product, I would get the sales of 4 days are SAPATOS and the rest with SHOES.

    I understand that being noSQL does not accept JOIN, however I see that there are "ways" to perform JOINs, maybe looking for the IDs (assuming the case that emulates the relational tables) individually after bringing the header table .

    I hope I have been clear enough and I know that more than one will have gone through this problem and I appreciate the hand they can give me to resolve it.

    asked by OTANO 30.09.2018 в 17:33

    1 answer


    Of course I never get the answer, because after reading a lot and doing several tests, what I want to do is not possible or rather should not be done because it is exactly the definition of relational databases.

    There are some emulators that try to go through the data and update but they are slow and in the end it ends up being better to use SQL.

    If what you need is to save the relationship as explained in the question, use an SQL engine, since a document-based engine does not work for this purpose.

    answered by 19.10.2018 / 17:40