[英]mongoDB deletes array after put operation
I have a controller:我有一个控制器:
exports.chooseTransporter = (req, res, next) => {
const transporter = new Signup({
_id: req.params.signupId,
approved: req.body.approved
});
Signup.updateOne({ _id: req.params.signupId}, transporter).then(result => {
res.status(201).json({
message: "Signup updated!",
result: result
});
})
.catch(err => {
res.status(500).json({
message: "DB error!"
});
});
}
and model:和型号:
const mongoose = require("mongoose");
const signupSchema = mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
cargoId: { type: mongoose.Schema.Types.ObjectId, ref: "Cargo", required: true },
truckIds: [{type: mongoose.Schema.Types.ObjectId, ref: "Truck", required: true }],
approved: { type: Boolean ,required: true },
finished: { type: Boolean ,required: true }
});
module.exports = mongoose.model("Signup", signupSchema);
After creating a signup my collection looks like below:创建注册后,我的收藏如下所示:
{
"_id" : ObjectId("616eb5061aeeabc39184696d"),
"truckIds" : [
ObjectId("6131f12e8ed8b949f6752c59"),
ObjectId("6137150e1b92ae2bf762f87b")
],
"userId" : ObjectId("60868c31caafe530e7e2d04a"),
"cargoId" : ObjectId("614da0b0812a2f6169163d37"),
"approved" : false,
"finished" : false,
"__v" : 0
}
After running a chooseTransporter method and setting approved to true, my truckIds array is 0 elements like below:在运行chooseTransporter 方法并将批准设置为true 后,我的truckIds 数组为0 个元素,如下所示:
{
"_id" : ObjectId("616eb5061aeeabc39184696d"),
"truckIds" : [],
"userId" : ObjectId("60868c31caafe530e7e2d04a"),
"cargoId" : ObjectId("614da0b0812a2f6169163d37"),
"approved" : true,
"finished" : false,
"__v" : 0
}
Thanks in advance!提前致谢!
Try to change the updateOne
parameters and specify only the approved
attribute:尝试更改
updateOne
参数并仅指定approved
属性:
Signup.updateOne({ _id: req.params.signupId }, { approved: req.body.approved })
.then((result) => {
res.status(201).json({
message: 'Signup updated!',
result: result,
});
})
.catch((err) => {
res.status(500).json({
message: 'DB error!',
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.