Below is my Schema and API
Schema.js file
const structure = new mongoose.Schema({
Name: {
type:String,
required:[true,"Please Enter Task"],
},
Completed: {
type:Boolean,
default:false
}
})
let work = mongoose.model('tasks',structure)
module.exports = work
I have imported it in the api file and wrote an api to update records
const work = require("../models/schema.js")
async function updateone(req,res)
{
console.log(req.body)
try {
let treat = {"First" :"Michael" }
let newtask = await
work.findOneAndUpdate({_id:req.params.id},treat,{ overwrite: true })
res.status(200).json(newtask)
}
catch (error) {
res.status(500).json({status:"Fail",msg:"Wrong Parameter"})
}
}
Now , I want that upon updating , only name shall remain and Completed
field should not be there. I know that mongoose by default wraps it in $set
therefore I used overwrite:true
so that my entire document will be replaced.
However , I am seeing that the value Name
is replaced but the Completed
field is still present. Also I noticed that if I provide a totally new field then nothing changes.
It means if :-
let treat = {"Job" :"Pilot" }
let newtask = await work.findOneAndUpdate({_id:req.params.id},treat,{ overwrite: true })
In this case nothing changes , no new field is added at all.What am I doing wrong here ?