[英]Update MongoDB object in object by id
Model: 模型:
const projectSchema = new Schema({
name: {
type: String,
maxlength: 50
},
description: {
type: String
},
projectLead: {
type: String
},
start: {
type: String
},
end: {
type: String
},
projectType: {
type: String
},
claimId: {
type: String
},
organization: {
type: String
},
seats: [{
id: String,
employee: String,
role: String,
start: String,
end: String,
workload: Number,
skills: Array,
approved: Boolean
}]
}, {
timestamps: true
})
Model response example: 模型响应示例:
{
"project": {
"_id": "5cab4b9bc9b29a7ba2363875",
"name": "Project Title",
"description": "Project description",
"projectLead": "email@email",
"start": "2018-06-01T09:45:00.000Z",
"end": "2019-06-31T09:45:00.000Z",
"claimId": "AIIIIII",
"organization": "Company ACE",
"seats": [
{
"skills": [
"Node.js",
"Vue.js"
],
"_id": "5cab548e5cefe27ef82ca313",
"start": "2018-06-01T09:45:00.000Z",
"end": "2019-06-31T09:45:00.000Z",
"role": "Developer",
"approved": false,
"workload": 20,
"employee": ''
}
],
"createdAt": "2019-04-08T13:24:43.253Z",
"updatedAt": "2019-04-08T14:02:54.257Z",
"__v": 0
}
}
Controller: 控制器:
exports.updateSeatEmployee = async (req, res, next) => {
try {
const project = await Project.findOneAndUpdate({
"seats._id": req.params.id
}, {
"seats.employee": req.body.employee
}, {
new: true
})
console.log("project", project);
return res.json({
project: project
})
} catch (error) {
next(error)
}
}
Debugger: 调试器:
Mongoose: users.createIndex({ email: 1 }, { unique: true, background: true }) { sub: '5cab48ebec24577ab3329fcd', iat: 1554729210 }
猫鼬:users.createIndex({电子邮件:1},{唯一:true,背景:true}){子目录:'5cab48ebec24577ab3329fcd',iat:1554729210}
Mongoose: users.findOne({ _id: ObjectId("5cab48ebec24577ab3329fcd") }, { projection: {} })
猫鼬:users.findOne({_id:ObjectId(“ 5cab48ebec24577ab3329fcd”)},{投影:{}})
Mongoose: projects.findAndModify({ 'seats._id': ObjectId("5cab529bcafa027e30ee229c") }, [], { '$setOnInsert': { createdAt: new Date("Mon, 08 Apr 2019 14:45:56 GMT") }, '$set': { 'seats.$.employee': 'email@email.com', updatedAt: new Date("Mon, 08 Apr 2019 14:45:56 GMT") } }, { new: true, upsert: false, remove: false, projection: {} }) (node:33302) DeprecationWarning: collection.findAndModify is deprecated.
猫鼬:projects.findAndModify({'seats._id':ObjectId(“ 5cab529bcafa027e30ee229c”)},[],{'$ setOnInsert':{createdAt:new Date(“ Mon,08 Apr 2019 14:45:56 GMT”) },'$ set':{'seats。$。employee':'email@email.com',UpdatedAt:new Date(“ Mon,08 Apr 2019 14:45:56 GMT”)}},{new:true ,upsert:false,remove:false,投影:{}})(节点:33302)DeprecationWarning:不推荐使用collection.findAndModify。 Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
请改用findOneAndUpdate,findOneAndReplace或findOneAndDelete。
project null
项目为空
I want to search for a specific object in seats
by its id
. 我想通过其
id
搜索seats
的特定对象。 This specific seat should be update. 这个特定的座位应该更新。 In my case I want to update the
employee
field. 就我而言,我想更新
employee
字段。
If I do a findOne({"seats._id": req.params.id})
I get the project back but findOneAndUpdate
returns null
. 如果我执行
findOne({"seats._id": req.params.id})
我将返回项目,但findOneAndUpdate
返回null
。
You need the $ positional operator since seats
is an array and you should use $set operator if you want to update just one field 您需要$位置运算符,因为
seats
是一个数组,如果只想更新一个字段,则应该使用$ set运算符
const project = await Project.findOneAndUpdate({
"seats._id": req.params.id
}, {
$set: { "seats.$.employee": req.body.employee }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.