DocumentDB:如何更新部分属性值

时间:2016-06-28 18:30:31

标签: azure-cosmosdb

我一直在努力解决这个问题。这是我的文件。

{
  "id": "1",
  "Scenario": "Welcome page",
  "Translations": [
    {
      "Language": "En",
      "Content": "Welcome!"
    },
    {
      "Language": "Fr",
      "Content": "Bienvenue!"
    }
  ],
  "LastModified": "2016-05-27T17:27:58.562-06:00",
  "ModifiedBy": "admin",
  "LastAccessed": "2016-06-13T10:27:58.562-06:00"
}

这是我更新属性的代码(我已经在这里对要修改的值进行了硬编码):

SqlQuerySpec query = new SqlQuerySpec()
{
    QueryText = "SELECT c.id, c.Scenario, t.Language, t.Content, c.LastModified, c.ModifiedBy, c.LastAccessed FROM MultiLanguage as c join t in c.Translations where c.id = @Id and t.Language = @Language",
    Parameters = new SqlParameterCollection() { new SqlParameter("@Id", Id), new SqlParameter("@Language", Language) }
};

Document doc = client.CreateDocumentQuery<Document>(
    collectionLink, query).AsEnumerable().FirstOrDefault();

doc.SetPropertyValue("Content", "Welcome to Stackoverflow!");

Document updated = await client.ReplaceDocumentAsync(doc);

此函数传递了两个参数。身份和语言。例如,我只想将"Welcome to Stackoverflow!"Id = "1"的属性内容更新为Language="En"。问题是我的代码会移除其他"Language": "Fr""Content": "Bienvenue!"部分,并将我的文档更新为以下内容:

{ 
    "id": "1",
    "Scenario": "Welcome page",
    "Language": "En",
    "Content": "Welcome to Stackoverflow!",
    "LastModified": "2016-05-27T17:27:58.562-06:00",
    "ModifiedBy": "admin",
    "LastAccessed": "2016-06-13T10:27:58.562-06:00"
}

但我想要的是:

{
    "id": "1",
    "Scenario": "Welcome page",
    "Translations": [
      {
        "Language": "En",
        "Content": "Welcome to Stackoverflow!"
      },
      {
        "Language": "Fr",
        "Content": "Bienvenue!"
      }
    ],
    "LastModified": "2016-05-27T17:27:58.562-06:00",
    "ModifiedBy": "admin",
    "LastAccessed": "2016-06-13T10:27:58.562-06:00"
}

那么我怎样才能更新文档的部分属性而不会弄乱其他属性?

1 个答案:

答案 0 :(得分:0)

DocumentDB没有更新。您必须阅读文档,更新客户端,然后将其重新保存在旧文档的顶部。或者,您可以将您的翻译列表反规范化为单独的文档,并将“外键”返回到与其相关的场景。