c# - 更新我的 Documentdb 中的属性值

标签 c# azure azure-cosmosdb

我的文档中有以下结果。 我想更新“Type”和“Workout”的属性值,这样它就不会影响其他值

"PartyId": 10114795,
"WorkoutId": 4,
"Type": "heart-rate-zone-target",
"DateStarted": "2016-02-05T18:14:15.1620890Z",
"id": "0c44d1ee-58b8-4083-a702-f770ba0e63e9",
"_rid": "0ThoANQ4YwEEAAAAAAAAAA==",
"_self": "dbs/0ThoAA==/colls/0ThoANQ4YwE=/docs/0ThoANQ4YwEEAAAAAAAAAA==/",
"_etag": "\"08000130-0000-0000-0000-58dd72da0000\"",
"_attachments": "attachments/",
"_ts": 1490907866

最佳答案

I wanted to update the property value for "Type" and "Workout", such that it doesn't effect other values.

我做了一个演示,只是为了更新指定的属性。以下是我的详细步骤:

1.创建C#控制台项目并引用Azure DocumentDB SDK

2.添加TestModel类

 public class TestModel
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }

        // used to set expiration policy
        [JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
        public int? TimeToLive { get; set; }
        public string PartyId { get; set; }
        public string Type { get; set; }
        public DateTime DateStarted { get; set; }
        public int WorkoutId { get; set; }

    }

3.将文档添加到集合

 private static readonly string databaseName = "tomtest";
 private static readonly string collectionName = "colltest";
  // Read config
 private static readonly string endpointUrl = ConfigurationManager.AppSettings["EndPointUrl"];
 private static readonly string authorizationKey = ConfigurationManager.AppSettings["AuthorizationKey"];

        var client = new DocumentClient(new Uri(endpointUrl),authorizationKey);
        Console.WriteLine();
        Console.WriteLine("**** Create Documents ****");
        Console.WriteLine();
        var document1Definition = new TestModel
        { 
            Id= Guid.NewGuid().ToString(),
            PartyId = "10114795",
            Type = "heart-rate-zone-target",
            DateStarted = DateTime.Now,
            WorkoutId = 4
        };

        var database = client.CreateDatabaseIfNotExistsAsync(new Database {Id = databaseName}).Result.Resource;
        var collection = client.CreateDocumentCollectionIfNotExistsAsync(
            UriFactory.CreateDatabaseUri(databaseName), new DocumentCollection
            {
               Id = collectionName
            }).Result.Resource;
        //create document
        var createdocument = client.CreateDocumentAsync(collection.SelfLink, document1Definition).Result.Resource; 

4.从Azure门户查看结果

enter image description here

5.查询创建的文档

 //query document
 var querydoc = client.CreateDocumentQuery<TestModel>(
 UriFactory.CreateDocumentCollectionUri(databaseName, collectionName))
            .Where(x => x.PartyId == "10114795")
            .AsEnumerable()
            .First();

6.更新文档。

//update document
querydoc.Type = "updateType";
querydoc.WorkoutId = 0;
var result = client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, querydoc.Id), querydoc).Result.Resource;
            Console.WriteLine(result);
            Console.WriteLine($"Update document Type :{ result.GetPropertyValue<string>("Type")} , WorkoutId:{result.GetPropertyValue<int>("WorkoutId")}");

7.从 Azure 门户和控制台检查。

enter image description here

enter image description here

包.config

<packages>
  <package id="Microsoft.Azure.DocumentDB" version="1.13.1" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
</packages>

关于c# - 更新我的 Documentdb 中的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43236821/

相关文章:

Azure DocumentDb 扁平建模与嵌套建模

c# - 自定义控件在两种样式之间切换

c# - SharpDX XAudio2:6 SourceVoice限制

使用 Azure 静态应用程序部署时, Angular 应用程序的路由不起作用

azure - Microsoft 认知 - 人脸 API - 人脸验证 - 工作原理

azure - 如何在 CosmosDB 中使用 Join 操作/子查询

c# ".datasource"未显示在框中

c# - 准备好的语句 C# : Impossible casting of System. System.Data.SQLite.SQLiteParameter 上的 Data.SqlClient.SqlParameter 对象?

azure - 更新 Azure Blob 存储中的元数据 BlockBlob

azure - 如何将 CosmosDb SQL 帐户作为二头肌模块参数传递?