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

239
Vistas
trying to update single entry in mongodb using node js

const updateTask = async (req, res) => {
  const { id } = req.params;
  let update = {};
  if (req.body.taskTitle) update.taskTitle = req.body.taskTitle;
  if (req.body.taskContent) update.taskContent = req.body.taskContent;
  if (req.body.role) update.role = req.body.role;
  
  let task = await Task.updateOne(
    { taskId: id },
      {
        $set: {
        update,
       },
    },
    { runValidators: true }
  );
};

This is my code to update my data in database

as I am trying to update single single data or key if I want to update single data but it's not updating any thing i don't know where its not working as i tried to console data data come perfectly

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

0

what the variable Task is ? And I’m sorry, but I didn’t really understand your question, so could you rephrase it more clearly ?

If not, be careful, users might inject things into your database. You should use the mongo-sanitize script.

Like that :

  • And check if your id is in the right form. If in your database it is not in bjson with MongoId. If this is the case, do not hesitate to convert your id as realized below.
const sanitize = require('mongo-sanitize');
const mongo = require('mongodb');  

const updateTask = async (req, res) => {
  const { id } = req.params;
  let update = {};
  if (req.body.taskTitle) update.taskTitle = sanitize(req.body.taskTitle);
  if (req.body.taskContent) update.taskContent = sanitize(req.body.taskContent);
  if (req.body.role) update.role = sanitize(req.body.role);
  
  let task = await Task.updateOne(
    { taskId: mongo.ObjectId(sanitize(id)) }, // You can remove the mongo.ObjectId if your id is not a objectid.
      {
        $set: {
        update,
       },
    },
    { runValidators: true }
  );
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

const updateTask = async (req, res) => {
  const { id } = req.params;
  let update = {};
  if (req.body.taskTitle) update.taskTitle = req.body.taskTitle;
  if (req.body.taskContent) update.taskContent = req.body.taskContent;
  if (req.body.role) update.role = req.body.role;
  
  let task = await Task.updateOne(
    { taskId: id },
      {
        $set: update,           
    },
    { runValidators: true }
  );
};

all you have to do was remove curly brackets and it will work like a charm $set : update

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