Skip to content

Commit

Permalink
update to add children while updating
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinovpankaj committed Oct 31, 2024
1 parent 94b0a03 commit 886378a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions model/projectDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,31 @@ module.exports = {
);
},
addUpdateProjectChild : async (projectId, childId, childData)=>{
return await mongo.Projects.findOneAndUpdate({_id:ObjectId(projectId),"children._id":ObjectId(childId)},
{
$set:{
"children.$":childData
var found = await mongo.Projects.findOne({_id:ObjectId(projectId),"children._id":ObjectId(childId)});
if(found){
try {
var result = await mongo.Projects.findOneAndUpdate({_id:ObjectId(projectId),"children._id":ObjectId(childId)},
{
$set:{
"children.$":childData
}
},{upsert:true}
);
return result;
}
},{upsert:true}
);
catch (error) {

}
}else{
return await mongo.Projects.updateOne({ _id: new ObjectId(projectId) }, {
$push: {
children: {
"_id": new ObjectId(childId),
...childData
}
}
});
}

}
};

0 comments on commit 886378a

Please sign in to comment.