Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

395
Vistas
MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id'

I was trying to make a CRUD app in Nodejs using MongoDB but when I wrote the update part and send the send using postman it's showing the error in the terminal that

**MongoServerError: Performing an update on the path '_id' would modify the immutable field '_id'**

Here's my code to update an entry bu _id in mongo db

    router.put('/api/employee/edit/:id' ,(req,res) =>{

    const emp = new Employee({
        
        
        name:req.body.name,
        email:req.body.email,
        salary:req.body.salary
    });
    Employee.findByIdAndUpdate(req.params.id, {$set: emp},(err,data) => {

        if(!err){
                res.status(200).json({code:200,message:"Employee updated successfully",
            updateEmployee:data
            
            })
           

        }
        else{
            console.log(err);

                
        }

    })

    })
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I’m assuming giving the whole object as an argument to $set means it will try to modify all properties of it, including _id which shouldn’t be modified and you haven’t set on the object. Try to specify the properties to modify separately:

Employee.findByIdAndUpdate(req.params.id, {$set: {
    name:req.body.name,
    email:req.body.email,
    salary:req.body.salary
}},(err,data) => {
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are creating a new Employee document and wants update the current one with that new one. You can not do that since that will also try to override _id of the document. Instead of creating a new Employee document, you should just update existing one with new data:

router.put('/api/employee/edit/:id', (req, res) => {

  Employee.findByIdAndUpdate(req.params.id, {
    name: req.body.name,
    email: req.body.email,
    salary: req.body.salary
  }, { new: true }, (err, data) => {
    
    if (!err) {
      res.status(200).json({ code: 200, message: "Employee updated successfully", updateEmployee: data })
    } else {
      res.status(400).json({ code: 400, message: "Employee updated failed." })
    }
  })
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda