Ashwin Joshy
Ashwin Joshy

Reputation: 49

How to build a Moongoose Schema for this?

I am doing a hostel project so now i am to build room allocation ,the response for a get request is as follows

{
    "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"]}]}

i dont even know how to build a schema for this,i did built some other schema but this one is different i guess,so if someone helped me with this,it would be great

Upvotes: 0

Views: 32

Answers (2)

amda
amda

Reputation: 354

@nidhi_007's answer is correct, but you could go a little bit further with the definition of the category_allocation field by doing:

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]
]

Upvotes: 1

nidhi_007
nidhi_007

Reputation: 31

You can modify response object as following :

{
    "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"]}
]
}

Appropriate schema can be :

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
}

Upvotes: 1

Related Questions