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

146
Vistas
Update mongo collection with values from a javascript map

I have a collection that looks like this

[
   {
      "project":"example1",
      "stores":[
         {
            "id":"10"
            "name":"aa",
            "members":2
         }
      ]
   },
   {
      "project":"example2",
      "stores":[
         {
            "id":"14"
            "name":"bb",
            "members":13
         },
         {
            "id":"15"
            "name":"cc",
            "members":9
         }
      ]
   }
]

I would like to update the field members of the stores array taking getting the new values from a Map like for example this one

0:{"10" => 201}
1:{"15" => 179}

The expected result is:

[
       {
          "_id":"61",
          "stores":[
             {
                "id":"10"
                "name":"aa",
                "members":201
             }
          ]
       },
       {
          "_id":"62",
          "stores":[
             {
                "id":"14"
                "name":"bb",
                "members":13
             },
             {
                "id":"15"
                "name":"cc",
                "members":179
             }
          ]
       }
    ]

What are the options to achieve this using javascript/typescript?

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

0

You can use update() function of Mongoes model to update your expected document. Try following one:

const keyValArray= [{"10": 201},{"15":"179"}];
db.collectionName.update({_id: givenId},
       { $push: { "stores": {$each: keyValArray} }},
       function(err, result) {
          if(err) {
             // return  error
          }
          //return success
       }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

In the end, I resolved by updating the original database entity object with the values in the map, then I have generated a bulk query.

for (let p of projects) {
    for(let s of p.stores) { 
        if(storeUserCount.has(s.id)){
            s.members = storeUserCount.get(s.id);
        }
    };
    bulkQueryList.push({ 
        updateOne: {
            "filter": { "_id": p._id },
            "update": { "$set": {"stores": p.stores} }
        }});
};

await myMongooseProjectEntity.bulkWrite(bulkQueryList);
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