Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!