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

178
Vistas
Mongoose not updating multiple matches
[
  {
    "id": 1,
    "items": [
      {
        id: 15,
        score: 10
      },
      {
        id: 14,
        score: 100
      },
      {
        id: 12,
        score: 1
      }
    ]
  },
  {
    "id": 2,
    "items": []
  }
]

Now, I try to update items whose id is 14,15 & used the following query.

db.collection.update({
  "items.id": {
    $in: [
      14,
      15
    ]
  }
},
{
  $set: {
    "items.$.score": 444
  }
},
{
  multi: true
}
)

but it updated only the first match in items that is that is id with 15, what can be wrong?

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "id": 1,
    "items": [
      {
        "id": 15,
        "score": 444
      },
      {
        "id": 14,
        "score": 100
      },
      {
        "id": 12,
        "score": 1
      }
    ]
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "id": 2,
    "items": []
  }
]
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can do it with Positional identifiers $[]

db.collection.update({
  "items.id": {
    $in: [
      14,
      15
    ]
  }
},
{
  $set: {
    "items.$[element].score": 444
  }
},
{
  arrayFilters: [
    {
      "element.id": {
        $in: [
          14,
          15
        ]
      }
    }
  ],
  multi: true
})

try it here

over 4 years ago · Santiago Trujillo Denunciar

0

Use arrayFilters with targeted array elements: here is the doc

db.collection.update({
  "id": 1
},
{
  "$set": {
    "items.$[ele].score": 20
  }
},
{
  arrayFilters: [
    {
      "ele.id": {
        "$in": [
          15,
          14
        ]
      }
    }
  ]
})

see play ground code https://mongoplayground.net/p/cdgu1aqLwsI

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