Search code examples
databasemongodbmongodb-querynosql

Should I nest elements that I will consult frequently in no-sql?


I have a non-relational database in MongoDB where I store items with the following structure:

    {
      "_id": "64586748c079e955385c18af",
      "operator": "KILIN",
      "department": "EXAMPLE-DEPARMENT",
      "city": "MIAMI",
      "locality": "Usaquén",
      "neighborhood": "LISBOA",
      "start_date": "2023-04-29",
      "final_date": "2023-04-29",
      "reason": "programed",
      "description": "AV 88 # 8123",
      "status": "not_validated",
      "customers_affected": [
        {"ENEL-BOG-00013":{"name": "tecniglas", "Phone": 3456752}},
        {"ENEL-BOG-00111":{"name": "pepsi", "Phone": 2354321}},
      ]
    }

The database deals with many service outage alerts. My question is focused on the "customers_affected" element. There are a number of customers in the country, and each alert can affect them depending on their location. I will constantly have to be querying the customers for each alert to send notifications (either by sms or email). My doubt is if I should leave it as I have it structured or if I should save in another collection all the clients and link them somehow with the alerts.


Solution

  • List out your pros & cons e.g. (nb: I'm not claiming this is exhaustive)

    Option 1: Keep customer data within the same document

    Pros:

    • Easier to query and update customer data in a single operation.
    • Less data duplication, as customer data is only stored once per customer.

    Cons:

    • Documents may become very large if there are many customers, which could impact query performance.
    • Customer data is not normalized, which may make it harder to manage and maintain.

    Option 2: Store customer data in a separate collection and link it with the alerts

    Pros:

    • Normalizes data, making it easier to manage and maintain.
    • Allows for better separation of concerns between alert data and customer data.
    • May improve query performance by reducing document size.

    Cons:

    • Requires additional queries to fetch related customer data, which could impact performance.
    • Requires maintaining relationships between alerts and customers, which may add complexity to the application.

    It's up to you to decide which option best suits your use case. If you have a relatively small number of customers and don't expect frequent updates, keeping the customer data within the same document might be a simpler and more efficient approach. If however you expect a large number of customers and frequent updates, storing customer data in a separate collection and linking it with the alerts might be a better choice for performance and maintainability. And: as you will constantly have to be querying the customers for each alert to send notifications, it may be better to store the customers in a separate collection and link them to the alerts using a unique identifier.

    NB: This is response is "generic", without much information on volumes or predicted growth and without any ability to perform any tests.