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

115
Vistas
MongoDB Get Field to Update a Value from Request Query

I have user object structured like this:

{
  "id": "",
  "username": "",
  "name": "",
  "bio": "",
  "email": "",
  "profileImg": "",
  "phoneNumber": 0,
  "messagingPoints": 0,
  "funds": 0,
  "inventoryId": "",
  "lastLogin": "2022-02-23T03:27:13.535Z",
  "isPrivate": false,
  "messagesReceived": []
}

I want to be able to reach a patch endpoint to update any of these fields. For example, /api/user?id=userId&name=John, should be able to grab the field "name" and set it to John. /api/user/id=?id=userId&email=abc@gmail.com should grab the email field and set it to abc@gmail.com

I am struggling to find docs for MongoDB to accomplish this, so I'm wondering if it is not possible? Do I need a specific endpoint for each of these update operations (ex: /api/user/name?id=userId&value=John instead of /api/user?id=userId&name=John)?

If it is possible, how could I accomplish this? Thanks!

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

0

You can pass user ID in update filter. Also you can pass data in request body of the PUT request instead of query parameters.

app.put('/api/user', async (req, res) => {
  const { id, ...data } = req.body; 
  // Filter out any invalid fields

  // Update documents where id field is equal to value of id in request body
  await collection.updateOne(
    { id },
    { $set: data }
  );

  return res.json({ data: "User updated" })
})
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