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 }
Mongoose: users.findOne({ _id: ObjectId("5cab48ebec24577ab3329fcd") }, { projection: {} })
Mongoose: projects.findAndModify({ 'seats._id': ObjectId("5cab529bcafa027e30ee229c") }, [], { '$setOnInsert': { createdAt: new Date("Mon, 08 Apr 2019 14:45:56 GMT") }, '$set': { 'seats.$.employee': '[email protected]', 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. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
project null
I want to search for a specific object in seats
by its id
.
This specific seat should be update. In my case I want to update the employee
field.
If I do a findOne({"seats._id": req.params.id})
I get the project back but findOneAndUpdate
returns null
.