Arrays 我可以修改MongoDB文档中数组的值,但它不';t在数据库中更新
我在Mondo DB的文件中有这个房间:Arrays 我可以修改MongoDB文档中数组的值,但它不';t在数据库中更新,arrays,node.js,mongodb,Arrays,Node.js,Mongodb,我在Mondo DB的文件中有这个房间: { assets: { tv: true, pc: true, whiteboard: false, projector: true }, _id: 5ddbbfbb8bbc7741aca695cc, name: 'Room 1', floor: '1', capacity: 10, bookings: [ { isValidated: false, _id: 5de6ba9211
{
assets: { tv: true, pc: true, whiteboard: false, projector: true },
_id: 5ddbbfbb8bbc7741aca695cc,
name: 'Room 1',
floor: '1',
capacity: 10,
bookings: [
{
isValidated: false,
_id: 5de6ba92112594491017b169,
startDate: 2019-12-03T23:00:00.000Z,
endDate: 2019-12-03T23:00:00.000Z,
startHour: 2019-12-03T11:00:37.862Z,
duration: 2019-12-03T02:30:37.862Z,
company: 'Google',
user: 5ddbca33fdf0e244c53af1b5
}
],
__v: 0
}
因此,我编写了这个函数,使用要更改的房间文档id和预订id将值更改为true:
router.put('/book/pending/:id/:_id', (req, res, next) => {
try {
const { id } = req.params
const { _id } = req.params
if (req.user.isAdmin == true) {
Room.findByIdAndUpdate(id).then((books) => {
console.log(books)
var j = 0
for (let index = 0; index < books.bookings.length; index++) {
if (books.bookings[index].isValidated == false && books.bookings[index]._id == _id) {
books.bookings[index].isValidated = true
j++
}
}
console.log(books.bookings)
res.status(200).json(books)
})
} else {
res.status(400).json("Cannot Change Book")
}
} catch (error) {
next(error)
}
})
但是我不知道如何在文档中保存数组。
有人能帮我吗:)?你试过使用
图书保存()或房间保存(图书)
?假设Room
是猫鼬类,上述任何一种都应该有效。有效!!!!!!我以前试过,但我改变了,因为它告诉我books.save()不是一个函数和房间。save()不是一个函数,但我认为这是因为我的函数是jsut findById,而不是findById和Update!谢谢你,兄弟!
{
assets: { tv: true, pc: true, whiteboard: false, projector: true },
_id: 5ddbbfbb8bbc7741aca695cc,
name: 'Room 1',
floor: '1',
capacity: 10,
bookings: [
{
isValidated: true,
_id: 5de6ba92112594491017b169,
startDate: 2019-12-03T23:00:00.000Z,
endDate: 2019-12-03T23:00:00.000Z,
startHour: 2019-12-03T11:00:37.862Z,
duration: 2019-12-03T02:30:37.862Z,
company: 'Google',
user: 5ddbca33fdf0e244c53af1b5
}
],
__v: 0
}