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

71
Vistas
Incrementing multiple array values in an object MongoDB

I've been at this for a while now. Trying to update multiple array values, in the same object, with a single query.

Database data looks like this:

  id: 616f5aca5f60da8bb5870e36
  title: "title"
  recommendations: {
    616f65705f60da8bb5870e37: [
      0: 25,
      1: "title 2"
    ],
    61e83b48e498cc5aae9dc741: [
      0: 22,
      1: "title 3"
    ]
  }

So far I've tried both preparing a statement like this:

const fetchQuery = "recommendations.616f65705f60da8bb5870e37.0 : 1, recommendations.61e83b48e498cc5aae9dc741.0 : 1";

const result = await videoGamesCollection.updateOne(
        { _id: ObjectId(req.body.parentId) },
        { $inc: fetchQuery }
      );

And using array filters:

const result = await videoGamesCollection.updateOne(
        { _id: ObjectId(req.body.parentId) },
        { $inc: {"recommendations.$[elem].0": 1} },
        {arrayFilters: [{"elem":{$in:[req.body.voteIds]}}]}
      );

But neither seems to work. Is this possible or is my only option converting to array of arrays instead?

Thanks!

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

0

You need to enclose recommendations.616f65705f60da8bb5870e37.0 in quotes, like:

const result = await videoGamesCollection.updateOne(
        { _id: ObjectId(req.body.parentId) },
        { 
          $inc: {
             "recommendations.616f65705f60da8bb5870e37.0": 1,
             "recommendations.61e83b48e498cc5aae9dc741.0": 1 
        }
      );
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