Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert record into array inside a document in Azure Cosmos DB

Im using Azure Cosmos DB to store data in the following format.

{
    "id": "id",
    "name": "Name",
    "items": [
        {
            "id": "id",
            "name": "name"
        }
    ]
}

Im using Azure Functions and JavaScript to add to this document db, and this is working as expected.

Now I would like to add a new product into the array products. Is this even possible, without recreating the whole document?

I would really just like to send two inputs like this.

Input 1

{
    "id": "A",
    "name": "Name A",
    "items": [
        {
            "id": "01",
            "name": "Item A"
        }
    ]
}

Input 2

{
    "id": "A",
    "name": "Name A",
    "items": [
        {
            "id": "02",
            "name": "Item B"
        }
    ]
}

Result

{
    "id": "A",
    "name": "Name A",
    "items": [
        {
            "id": "01",
            "name": "Item A"
        },
        {
            "id": "02",
            "name": "Item B"
        }
    ]
}

Maybe im using it wrong, maybe the products should have been normalized into a separate document?

like image 203
Martin at Mennt Avatar asked Oct 29 '22 21:10

Martin at Mennt


1 Answers

Is this even possible, without recreating the whole document?

Partial updates to a document is not possible. You will need to fetch the existing document, update it and then save the entire document again.

like image 191
Gaurav Mantri Avatar answered Nov 01 '22 08:11

Gaurav Mantri