如何首先获取整个文档,然后进行更改并保存整个文档

时间:2019-06-01 12:57:28

标签: asp.net asp.net-web-api asp.net-core azure-cosmosdb

我是cosmos db的新手,并基于cosmos db创建了一个项目。我创建了基本的crud操作。现在的问题之一是用户有更多的工作,工作可能具有不同的类别,并且在每个类别中都有不同的工作。

添加代码示例:

public async Task < Catalog > AddAsync(Guid id,
    user order) {
    var requestOptions =
        new RequestOptions {
            PartitionKey = new PartitionKey(order.Id.ToString())
        };

    catalogDocument = await _cosmosClient.UpsertDocumentAsync(
        UriFactory.CreateDocumentCollectionUri(
            _azureCosmosDbOptions.Value.DatabaseId, "list"), order, requestOptions);

    return (user)((dynamic) catalogDocument.Resource);
}

Json身体:

 "id": "8000003c-0001-fb00-b63f-84710c7967aa",

    "work": {

      "category": [
        {

          "subcategory": [
            {
               "work" :{
                    "id":"22",
                   "dec":"one work"
                }

json正文输入:

 "id": "8000003c-0001-fb00-b63f-84710c7967aa",

    "work": {

      "category": [
        {

          "subcategory": [
            {
               "work" :{
                    "id":"23",
                   "dec":"two work"
                }

实际输出:

 "id": "8000003c-0001-fb00-b63f-84710c7967aa",

    "work": {

      "category": [
        {

          "subcategory": [
            {
               "work" :{
                    "id":"23",
                   "dec":"two work"
                }

但预期输出:

 "id": "8000003c-0001-fb00-b63f-84710c7967aa",

    "work": {

      "category": [
        {

          "subcategory": [
            {
               "work" :{
                    "id":"22",
                   "dec":"one work"
                },
                "work" :{
                    "id":"23",
                   "dec":"two work"
                }

我需要更新而无需重写我的数据库。例如work1已经退出,进入work2之后,我的列表必须具有两个工作数据集。请给我一些想法 谢谢

1 个答案:

答案 0 :(得分:0)

如果我正确理解,您正在寻找部分更新。 Azure cosmos db现在不支持部分更新。虽然您可以使用存储过程来更新整个文档。

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

希望有帮助。