wonderful world
wonderful world

Reputation: 11599

How to update a few properties of many documents in CosmosDB/DocumentDB when another property change?

I have a shopping cart document and a list of products that the customer has added to the shopping cart. These documents live in the CosmosDB.

{
   "customer":"1000",
   "products":[
      {
         "Product":"2000",
         "Name":"iPhone 10"
      }
   ]
}

There is another document which has the details of the product 2000. Often the name of the product are updated. For example, the name can be changed to iPhone 10 from Apple. As result of that change, the product name in the shopping cart also need to be changed to reflect the change.

How can I update the shopping cart documents of many customers with the new pdocut name efficiently?

Upvotes: 1

Views: 1744

Answers (2)

Jay Gong
Jay Gong

Reputation: 23792

Firstly,i have to say that your data is so closely related that you should use relational database. Cosmos DB SQL API is a non-relational database, and the relationship between data is relatively loose. It's tough to change the data of other columns as one column changes.

Since your plan has been decided already, you need to update all the related other data which contains product name column if the product name changes. Or you could consider adjusting your data structure. For example, if product name will be changed continuously,you could save the product info as the main of data and the shopping cart info as sub-nested array in the product.

Like:

{
   "productId":"1",
   "Name":"iphone",
   "shopping cart":[
      {
         "customerId":"1000",
         "count":"1"
      },
      {
         "customerId":"2000",
         "count":"2"
      }
   ]
}

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222722

I think you need to re-look at the way you are saving the documents.It is really not optimal solution

I would recommend you to store everything in a single document with nested arrays. Here is a great example on How to model and partition data on Azure Cosmos DB using a real-world example

Upvotes: 1

Related Questions