gpt4 book ai didi

Azure Cosmos Db 文档 - 想要对文档中已删除的属性使用 ReplaceDocumentAsync

转载 作者:行者123 更新时间:2023-12-02 06:32:30 25 4
gpt4 key购买 nike

// I am using code like below
Document doc = client.CreateDocumentQuery<Document>(collectionLink)
.Where(r => r.Id == "doc id")
.AsEnumerable()
.SingleOrDefault();

doc.SetPropertyValue("MyProperty1", "updated value");
Document updated = await client.ReplaceDocumentAsync(doc);

想要从文档中删除“MyProperty2”。如何做到这一点?

最佳答案

您似乎想要更新 MyProperty1 属性并从文档中删除 MyProperty2 属性,以下示例代码供您引用。

private async Task updateDoc()
{
string EndpointUri = "xxxxx";
string PrimaryKey = "xxxxx";
DocumentClient client;

client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);

Document doc = client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri("testdb", "testcoll"))
.Where(r => r.Id == "doc5")
.AsEnumerable()
.SingleOrDefault();

//dynamically cast doc back to your MyPoco
MyPoco poco = (dynamic)doc;

//Update MyProperty1 of the poco object
poco.MyProperty1 = "updated value";

//replace document
Document updateddoc = await client.ReplaceDocumentAsync(doc.SelfLink, poco);


Console.WriteLine(updateddoc);
}

public class MyPoco
{
public string id { get; set; }
public string MyProperty1 { get; set; }
}

我的文档: enter image description here更新: enter image description here

编辑:

this would remove "MyProperty3" and "MyProperty4" as well.

正如您所提到的,将属性设置为 null 也是一种方法。

Document doc = client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri("testdb", "testcoll"))
.Where(r => r.Id == "doc5")
.AsEnumerable()
.SingleOrDefault();

doc.SetPropertyValue("MyProperty2", null);

//replace document
Document updateddoc = await client.ReplaceDocumentAsync(doc.SelfLink, doc);

关于Azure Cosmos Db 文档 - 想要对文档中已删除的属性使用 ReplaceDocumentAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com