Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you update sub-document in cosmos db

I am new to Cosmos Db and want to understand how to delete/upsert sub-documents within a document collection.

If i have a document:

{ "Id": "1234", "Name": "foo", "Items": [ { "Id": "abcd", "Age": 35, "Claims": [ { "Name": "email", "Value": "[email protected]" } ] } ] }

How do i:

1) add an item to the Items list in the document.

2) delete the existing Item from the Items list

3) upsert item to the items list in the document

4) add/delete a claim value to existing item in items list?

Thanks in advance.

like image 628
Jerrod Horton Avatar asked May 09 '18 02:05

Jerrod Horton


2 Answers

As @Sajeetharan said, azure cosmos db doesn't support partial updates now. It seems the team is actively working on this feature. Now,you could update entire document in stored procedure.

Sample code as below for reference:

function updateSproc(id, update) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();

    tryQueryAndUpdate();

    function tryQueryAndUpdate(continuation) {
        var query = {query: "select * from root r where r.id = @id", parameters: [{name: "@id", value: id}]};
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, documents, responseOptions) {
            if (err) throw err;

            if (documents.length > 0) {
                tryUpdate(documents[0]);
            } else {
                throw new Error("Document not found.");
            }
        });
    }

function tryUpdate(document) {
    var requestOptions = {etag: document._etag};

    var fields, i;

    fields = Object.keys(update);
    for (i = 0; i < fields.length; i++) {
       document[fields[i]] = update[fields[i]];
    }

    var isAccepted = collection.replaceDocument(document._self, document, requestOptions, function (err, updatedDocument, responseOptions) {
        if (err) throw err;
        response.setBody(updatedDocument);
    });
}

However, Azure Cosmos DB support MongoDB protocol. You could confirm supported features on Azure official page.

So,incremental operations are supported. Please refer to this link.

Hope it helps you.Any concern,please feel free to let me know.

like image 54
Jay Gong Avatar answered Oct 27 '22 17:10

Jay Gong


Currently there is no way rather than retrieving the document doing the modification and updating it.

However there is a User voice feature request has been updated on Mar 5, 2018:

UPDATE:

Partial Document Update was GAed in Ignite 2021, from now on you will be able to update the document locally and then send it over the wire as a whole document Replace API call and here are the supported operations

like image 28
Sajeetharan Avatar answered Oct 27 '22 16:10

Sajeetharan