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

125
Vistas
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 Respuestas
Responde la pregunta

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 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