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

127
Visualizações
How to iterate through nested document passed as a json body, and update every document in a collection

I am trying to update multiple documents in a collection for an ecommerce workshop for school and I don't know how to update the documents that needs their quantity changed according to how much the client has bought

this is how the json body is being received from frontend:

{
    "clientId": "123123",
    "cartItems": [
        { 
            "itemId":"6547",
            "quantity":"3" 
        }, {
            "itemId": "6543", 
            "quantity": "2"
        }
    ], 
    "address": "123 main street",
    
    "creditCardNumber":"1234567890123456"
}

I would like to iterate through the cartItems array and update the my Items collection in my database

Each document in my collection looks something like this:

{
    "_id":"6543",
    "name":"Barska GB12166 Fitness Watch with Heart Rate Monitor",
    "price":"$49.99",
    "body_location":"Wrist",
    "category":"Fitness",
    "imageSrc":"data:image/jpeg;base64...",
    "numInStock":"9",
    "companyId":"19962"
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

order.cartItems.forEach(async (item) => {
      const result = await db
        .collection("items")
        .updateOne(
          { _id: parseInt(item.itemId) },
          { $inc: { numInStock: -parseInt(item.quantity) } }
        );
    });

then to restore the numInStock

result.cartItems.forEach(async (item) => {
      const result = await db
        .collection("items")
        .updateOne(
          { _id: parseInt(item.itemId) },
          { $inc: { numInStock: parseInt(item.quantity) } }
        );
    });
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