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

105
Vistas
How to update sub array item in MongoDB, JavaScript

So let's say I have this piece of code:

const old = await Sports.findById(sportId);

And it returns the following:

{
    _id: "sdsdsds",
    name: "sdsdsds",
    enabled: true,
    markets: [
        {
            enabled: true,
            defId: "LOOKATME",
            name: "sdsdsd",
        },
        {
            enabled: false,
            defId: "dfsdfdsfs",
            name: "sdsdfsdsd",
        },
    ]
}

Now, let's say I want to update the market with the defId of "LOOKATME" so it enabled becomes false. Would it be possible to added inside an update function such as this:

const updated = await Sports.findByIdAndUpdate(old, [LOGIC], {new: true});

How would I go about doing this so the output is this:

{
    _id: "sdsdsds",
    name: "sdsdsds",
    enabled: true,
    markets: [
        {
            enabled: false, // this is what changed
            defId: "LOOKATME",
            name: "sdsdsd",
        },
        {
            enabled: false,
            defId: "dfsdfdsfs",
            name: "sdsdfsdsd",
        },
    ]
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const updated = await Sports.updateOne({_id: old._id, "markets.defId": "LOOKATME"}, {$set: {"markets.$.enabled": false}}, {new: true});
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