• Wed. Dec 7th, 2022

SoftwareMile.com

Latest Software News and Hacks

How to iterate through nested document passed as a json body, and update every document in a collection

Byadmin

May 31, 2022

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 `Items` 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" } 

note: I’m very new to MongoDB and still trying to wrap my head around it. Thanks in advance

submitted by /u/angelo_mab
[link] [comments]


Go to Source of this post
Author Of this post: /u/angelo_mab
Title Of post: How to iterate through nested document passed as a json body, and update every document in a collection
Author Link: {authorlink}