MongoDB Database Structure and Best Practices Help

后端 未结 1 2010
刺人心
刺人心 2021-02-13 11:05

I\'m in the process of developing Route Tracking/Optimization software for my refuse collection company and would like some feedback on my current data structure/situation.

1条回答
  •  花落未央
    2021-02-13 11:47

    You database schema looks like for me as 'classic' relational database schema. Mongodb good fit for data denormaliztion. I guess when you display routes you loading all related customers, driver, truck.

    If you want make your system really fast you may embedd everything in route collection.

    So i suggest following modifications of your schema:

    1. customers - as-is
    2. trucks - as-is
    3. drivers - as-is
    4. route-list:

      Embedd data about customers inside stops instead of reference. Also embedd truck. In this case schema will be:

       {
           "route_name": "monday_1",
           "day": "monday",
           "truck": {
               _id = 1,
               // here will be all truck data
           },
           "stops": [{
               "customer": {
                   _id = 1,
                   //here will be all customer data
               }
           }, {
               "customer": {
                   _id = 2,
                   //here will be all customer data
               }
           }]
       }
      
    5. routes:

      When driver starting new route copy route from route-list and in addition embedd driver information:

       {
           //copy all route-list data (just make new id for the current route and leave reference to routes-list. In this case you will able to sync route with route-list.)
           "_id": "1",
           route_list_id: 1,
           "start_time": "04:31 AM",
           "status": "active",
           driver: {
               //embedd all driver data here
           },
           "stops": [{
               "customer": {
                   //all customer data
               },
               "status": "complete",
               "start_time": "04:45 AM",
               "finish_time": "04:48 AM",
               "elapsed_time": "3"
           }]
       }
      

    I guess you asking yourself what do if driver, customer or other denormalized data changed in main collection. Yeah, you need update all denormalized data within other collections. You will probably need update billions of documents (depends on your system size) and it's okay. You can do it async if it will take much time.

    What benfits in above data structure?

    1. Each document contains all data that you may need to display in your application. So, for instance, you no need load related customers, driver, truck when you need display routes.
    2. You can make any difficult queries to your database. For example in your schema you can build query that will return all routes thats contains stops in stop of customer with name = "Bill" (you need load customer by name first, get id, and look by customer id in your current schema).

    Probably you asking yourself that your data can be unsynchronized in some cases, but to solve this you just need build a few unit test to ensure that you update your denormolized data correctly.

    Hope above will help you to see the world from not relational side, from document database point of view.

    0 讨论(0)
提交回复
热议问题