如何在cosmos db中更新子文档 [英] How do you update sub-document in cosmos db

查看:75
本文介绍了如何在cosmos db中更新子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cosmos Db的新手,想了解如何删除/上载文档集中的子文档.

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

如果我有文件:

{ "Id": "1234", "Name": "foo", "Items": [ { "Id": "abcd", "Age": 35, "Claims": [ { "Name": "email", "Value": "foo@bar.com" } ] } ] }

{ "Id": "1234", "Name": "foo", "Items": [ { "Id": "abcd", "Age": 35, "Claims": [ { "Name": "email", "Value": "foo@bar.com" } ] } ] }

我该怎么办

1)将一个项目添加到文档的项目"列表中.

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

2)从项目"列表中删除现有项目

2) delete the existing Item from the Items list

3)将项目追加到文档中的项目列表中

3) upsert item to the items list in the document

4)向项目列表中的现有项目添加/删除索赔值?

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

先谢谢了.

推荐答案

正如@Sajeetharan所说,azure cosmos db现在不支持partial updates.团队似乎正在积极地研究此功能.现在,您可以在stored procedure中更新整个文档.

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.

示例代码如下:

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);
    });
}

但是,Azure Cosmos DB支持MongoDB protocol.您可以在

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

因此,支持incremental operations.请参考此链接.

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

希望它对您有所帮助.有任何问题,请随时让我知道.

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

这篇关于如何在cosmos db中更新子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆