我试图删除"627cfc7a9765ad8c65d03798"房间">
obje:
_id:627b93d43c14903de411d915
name:"Awesome Hotel Updated"
type:"hotel"
city:"berlin"
tittle:"Best Hotel in the City"
rooms:[
0:"627cf8c384aa07450d629279"
1:"627cfc7a9765ad8c65d03798"
]
功能:
export const deleteRoom = async (req, res, next) => {
try {
await Room.findByIdAndDelete(req.params.id);
await Hotel.findByIdAndUpdate(req.params.hotelId, {
$pull: { rooms: req.params.id }
})
res.status(200).json("Room has been deleted.");
} catch (err) {
next(err)
}
}
帮我删除"房间"中的项目!
可能你正在寻找这个:
const object = {
_id:627b93d43c14903de411d915,
name:"Awesome Hotel Updated",
type:"hotel",
city:"berlin",
tittle:"Best Hotel in the City",
rooms:[
{room_id:"627cf8c384aa07450d629279"},
{room_id:"627cfc7a9765ad8c65d03798"}
]
}
Here in this object"您应该像上面提到的那样存储您的房间id ">room_id",这将帮助您轻松筛选房间
改为:
export const deleteRoom = async (req, res, next) => {
try {
const room = await Room.findById(req.params.id);
const hotel = await Hotel.findById(req.params.hotelId)
await room.remove()
hotel.rooms.splice(hotel.rooms.findIndex(a => a.room_id === room.id) , 1)
res.status(200).json("Room has been deleted.");
} catch (err) {
next(err)
}
}
下面的代码可以工作。
db.collection.update({
"city": "berlin"
},
{
$pull: {
"rooms": "627cf8c384aa07450d629279"
}
})
也许你在findByIdAndUpdate
方法中得到一个错误