【问题标题】:How to build a Moongoose Schema for this?如何为此构建 Mongoose Schema?
【发布时间】:2021-08-27 00:31:13
【问题描述】:

我正在做一个宿舍项目,所以现在我要建立房间分配,获取请求的响应如下

{
    "Total_room":646,
    "Allocated":497,
    "Unallocated":149,
    "Unallocated_list":[{"list":["505-646"]}],
    "Occupied":300,
    "Avaliable":197,
    "Mode":"Category",
    "Category_allocation":[{"name":"1st year B.tech Students","Batch":"2020-2024","Room":"001-088","Registered_users":102,"Allocated_rooms":95,"Room_no":["001-088","498-504"],"Occupied":66,"Room_array":["001","002","003","004","006","005","007","008","009","010","011","012","013","014","015","016","017","018","019","020","021","022","023","024","025","026","027","028","029","030","031","032","033","034","035","036","037","038","039","040","041","042","043","044","045","046","047","048","049","050","051","052","053","054","055","056","057","058","059","060","061","062","063","064","065","066"]},
        {"name":"2nd year B.tech Students","Batch":"2019-2023","Room":"001-088","Registered_users":98,"Allocated_rooms":90,"Room_no":["089-178"],"Occupied":4,"Room_array":["089","090","091","092"]},     
          {"name":"3rd year B.tech Students","Batch":"2018-2022","Room":"001-088","Registered_users":122,"Allocated_rooms":60,"Room_no":["179-238"],"Occupied":3,"Room_array":["179","180","181"]},
        {"name":"4th year B.tech Students","Batch":"2017-2021","Room":"001-088","Registered_users":67,"Allocated_rooms":80,"Room_no":["239-318"],"Occupied":15,"Room_array":["239","240","241","242","243","244","245","246","247","248","249","250","251","252","253","254"]},
        {"name":"1st year M.tech Students","Batch":"2020-2022","Room":"001-088","Registered_users":46,"Allocated_rooms":26,"Room_no":["319-344"],"Occupied":1,"Room_array":["319"]},
        {"name":"2nd year M.tech Students","Batch":"2021-2023","Room":"001-088","Registered_users":51,"Allocated_rooms":32,"Room_no":["345-376"],"Occupied":20,"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]},
        {"name":"Others","Room":"001-088","Allocated_rooms":66,"Registered_users":102,"Occupied":49,"Room_no":["377-442"],"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]},
        {"name":"Tutors","Room":"001-088","Allocated_rooms":25,"Registered_users":48,"Occupied":11,"Room_no":["443-467"],"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]},
        {"name":"Staff","Room":"001-088","Allocated_rooms":30,"Registered_users":35,"Occupied":3,"Room_no":["468-497"],"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]}]}

我什至不知道如何为此构建一个架构,我确实构建了一些其他架构,但我猜这个是不同的,所以如果有人帮我解决这个问题,那就太好了

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    @nidhi_007 的回答是正确的,但是您可以通过以下方式进一步定义 category_allocation 字段:

    category_allocation: [
      name: String,
      room: String,
      batch: {
        type: String,
        required: false
      },
      allocated_rooms: Integer,
      registered_users: Integer,
      occupied: Integer,
      room_no: [String],
      room_array: [String]
    ]
    

    【讨论】:

      【解决方案2】:

      您可以修改响应对象如下:

      {
          "Total_room":646,
          "Allocated":497,
          "Unallocated":149,
          "Unallocated_list":["505-646"],
          "Occupied":300,
          "Avaliable":197,
          "Mode":"Category",
          "Category_allocation":[{"name":"1st year B.tech Students","Batch":"2020-2024","Room":"001-088","Registered_users":102,"Allocated_rooms":95,"Room_no":["001-088","498-504"],"Occupied":66,"Room_array":["001","002","003","004","006","005","007","008","009","010","011","012","013","014","015","016","017","018","019","020","021","022","023","024","025","026","027","028","029","030","031","032","033","034","035","036","037","038","039","040","041","042","043","044","045","046","047","048","049","050","051","052","053","054","055","056","057","058","059","060","061","062","063","064","065","066"]},
              {"name":"2nd year B.tech Students","Batch":"2019-2023","Room":"001-088","Registered_users":98,"Allocated_rooms":90,"Room_no":["089-178"],"Occupied":4,"Room_array":["089","090","091","092"]},     
                {"name":"3rd year B.tech Students","Batch":"2018-2022","Room":"001-088","Registered_users":122,"Allocated_rooms":60,"Room_no":["179-238"],"Occupied":3,"Room_array":["179","180","181"]},
              {"name":"4th year B.tech Students","Batch":"2017-2021","Room":"001-088","Registered_users":67,"Allocated_rooms":80,"Room_no":["239-318"],"Occupied":15,"Room_array":["239","240","241","242","243","244","245","246","247","248","249","250","251","252","253","254"]},
              {"name":"1st year M.tech Students","Batch":"2020-2022","Room":"001-088","Registered_users":46,"Allocated_rooms":26,"Room_no":["319-344"],"Occupied":1,"Room_array":["319"]},
              {"name":"2nd year M.tech Students","Batch":"2021-2023","Room":"001-088","Registered_users":51,"Allocated_rooms":32,"Room_no":["345-376"],"Occupied":20,"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]},
              {"name":"Others","Room":"001-088","Allocated_rooms":66,"Registered_users":102,"Occupied":49,"Room_no":["377-442"],"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]},
              {"name":"Tutors","Room":"001-088","Allocated_rooms":25,"Registered_users":48,"Occupied":11,"Room_no":["443-467"],"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]},
              {"name":"Staff","Room":"001-088","Allocated_rooms":30,"Registered_users":35,"Occupied":3,"Room_no":["468-497"],"Room_array":["345","346","347","348","349","350","351","352","247","248","249","250","251","252","253","254"]}
      ]
      }
      

      适当的架构可以是:

      total_rooms: {
       type: Integer
      },
      allocated_rooms: {
       type: Integer
      },
      unallocated_rooms: {
       type: Integer
      },
      unallocated_list: {
       type: Array
      },
      occupied: {
       type: Integer
      },
      available: {
       type: Integer
      },
      mode: {
       type: String
      },
      category_allcoation: {
       type: Array
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-01
        • 2019-02-20
        • 2021-09-10
        • 2017-03-09
        • 2012-12-25
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多