0

I'm starting to study mongodb, but I want to understand better when to use embedded or referenced documents. the project I'm trying to make is something similar to a POS (point of sale), working like:

Every time that someone make a purchase, it inserts on the database, but, there are costumers with N groups of stores and theses "groups of stores" have N stores and N POS. After this i want a database to update the prices in specific stores (not in groups) and make a summary of how many sales any POS made.

So, talking about perfomance what is the best design and why? here are some exemples that I made: Embedded :

{
"group1": [
    {
        "store_id": 1,
        "store1": "store_name",
        "POS": [
            {
                "id_POS": 1,
                "POS_name": "name_1",
                "purchases": [
                    {
                        "id": 1,
                        "date": "2022_10_05",
                        "time": "10:00:00"
                    },
                    {
                        "id": 2,
                        "date": "2022_10_05",
                        "time": "10:10:00"
                    }
                ]
            },
            {
                "id_POS": 2,
                "POS_name": "name_2",
                "purchases": [
                    {
                        "id": 1,
                        "date": "2022_10_05",
                        "time": "10:50:00"
                    },
                    {
                        "id": 2,
                        "date": "2022_10_05",
                        "time": "11:59:00"
                    }
                ]
            }
        ],
        "itens": [
            {
                "id_prod": 4,
                "prod_name": "avocado",
                "price": 2.5
            },
            {
                "id_prod": 5,
                "prod_name": "potato",
                "price": 1.5
            }
        ]
    }
]

}

Referenced: group of stores,POS, and itens collection:

{
"group1":{
    "stores":[
        {
            "store_id":1,
            "name":"store1",
            "POS":[
                {"POS":[
                    {"id_pos":1},
                    {"id_pos":2}
                ]}
            ],
            "itens":[
                {"id_prod":4},
                {"id_prod":5}
            ]
        }
    ]
}

}

{
"id_pos": 1,
"id_store": 1,
"purchases": [
    {
        "id": 1,
        "date": "2022_10_05",
        "time": "10:50:00"
    },
    {
        "id": 2,
        "date": "2022_10_05",
        "time": "11:59:00"
    }
]

}

{
"id_store": 1,
"itens":[{
    "id_prod": 4,
    "prod_name": "avocado",
    "price": 2.5
},
{
    "id_prod": 5,
    "prod_name": "potato",
    "price": 1.5
}]

}

  • Date/time values should **never** be stored as string, it's a design flaw. Store always proper `Date` objects. – Wernfried Domscheit Oct 06 '22 at 18:44
  • It mainly depends on your data size. One document can have a size up to 16MiByte. For smaller data, usually the embedded approach is better. A document oriented NoSQL MongoDB has typically less tables than according relational SQL database. – Wernfried Domscheit Oct 06 '22 at 18:47
  • Key `group1` sounds like dynamic field name. This should be avoided, use generic field names. The handling will be easier, for example when you like to create an index. – Wernfried Domscheit Oct 06 '22 at 18:49

0 Answers0