简体   繁体   中英

need mongoose query to search array inside array

So the assignment is to allot parking spot according to type of vehicle. If 2-wheeler - Motorcycle spot, 4Wheeler - compact&large spot, bus - 5 large spots

I need a optimise way to get a available parking spot ie. slot.occupied == false, and i want to avoid multiple for loops , because data will increase and will have multiple levels.

    "levelNo":"1",
    "row":{[
        "rowNo":"1",
        "slots":[{
            "slotNo":1,
            "slotType":"Motorcycle",
            "occupied":false
            },
            {
            "slotNo":2,
            "slotType":"Motorcycle",
            "occupied":false
            },
            {
            "slotNo":3,
            "slotType":"Motorcycle",
            "occupied":false
            },{
            "slotNo":4,
            "slotType":"Compact",
            "occupied":false
            },{
            "slotNo":5,
            "slotType":"Compact",
            "occupied":false
            },{
            "slotNo":6,
            "slotType":"Compact",
            "occupied":false
            },
            {
            "slotNo":7,
            "slotType":"Large",
            "occupied":false
            },
            {
            "slotNo":8,
            "slotType":"Large",
            "occupied":false
            }]
        },
        {
        "rowNo":"2",
        "slots":[{
            "slotNo":1,
            "slotType":"Motorcycle",
            "occupied":false
            },
            {
            "slotNo":2,
            "slotType":"Motorcycle",
            "occupied":false
            },
            {
            "slotNo":3,
            "slotType":"Motorcycle",
            "occupied":false
            },{
            "slotNo":4,
            "slotType":"Compact",
            "occupied":false
            },{
            "slotNo":5,
            "slotType":"Compact",
            "occupied":false
            },{
            "slotNo":6,
            "slotType":"Compact",
            "occupied":false
            },
            {
            "slotNo":7,
            "slotType":"Large",
            "occupied":false
            },
            {
            "slotNo":8,
            "slotType":"Large",
            "occupied":false
            }]

        }]

}

Model(Schema) - please suggest a better way to store such data

    levelNo:{
            type:String,
            required:true,
        },
    row:[{
            rowNo:{
                type:String,
                required:true,
            },
            slots:[
                {
                    slotNo:{
                        type:Number,
                        required:true,
                    },
                    slotType:{
                        type:String,
                        enum:['Motorcycle','Compact','Large']

                    },
                    occupied:{
                        type:Boolean,
                        required:true,
                    },
                    currentVehicle:{
                        type:String,
                    }               
                }],

        }],
        totalrows:{
            type:Number,
            required:true
        },
        levelIsFull:{
            type:Boolean,
            default:false,
        }

})

maybe you could make a variable that stores the type of vehicle searching for a parking spot. Then you would need only one for loop that would search for a corresponding parking spot that is also not occupied. You don't need to loop each floor, loop through all the parking spots and when you find it display the spot number and the level it is on.

Instead of storing the data for each level. Make a only a parking spot model and in it display the level it is on. So the model would look something like:

spotNumber: {
  occupied: false,
  type: "large",
  level: 1
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM