Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda