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

267
Vistas
How to perform update query on deeply nested JSON in mongodb?

I have a nested json data structure in mongodb which looks like:

{
    'tid': 1,
    'matches': [{
        'dord': 1,
        'matches': [{
                'tord': 1,
                'score': 11
            },
            {
                'tord': 2,
                'score': 12
            }
        ]
    },
    {
        'dord': 2,
        'matches': [{
                'tord': 1,
                'score': 21
            },
            {
                'tord': 2,
                'score': 22
            }
        ]
    }]
}

I want to update the row with "dord": 1 and "tord": 1 and change value of score from 11 to 100. How do I do this?

What I already tried:

db.collection.update({'tid': 1}, {'matches': {$elemMatch: {'dord': 1}}}, {'matches': { $elemMatch: {'tord': 1}}}, {'score': 100})
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Demo - https://mongoplayground.net/p/Mi2HnhzkPpE

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

The filtered positional operator $[] identifies the array elements that match the arrayFilters conditions for an update operation

db.collection.update({ "tid": 1 },
{ $set: { "matches.$[m].matches.$[t].score": 100  } },
{
  arrayFilters: [
    { "m.dord": 1 }, // to match where dord = 1
    { "t.tord": 1, "t.score": 11 } // and where tord = 1 and score = 11
  ]
})
over 4 years ago · Santiago Trujillo 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