gpt4 book ai didi

.net - [DocumentDB .NET SDK]替换时ReadDocumentAsync和CreateDocumentQuery有什么区别

转载 作者:行者123 更新时间:2023-12-05 03:07:05 26 4
gpt4 key购买 nike

请先看代码。

在步骤 8 中,该字段未更新。在步骤 11 和 12 中,该字段被更新。

我有 2 个问题。

替换时ReadDocumentAsync和CreateDocumentQuery有什么区别?

用DocumentQuery得到的值进行替换是否合适?

<小时/>

控制台应用程序

NuGet 包(Microsoft.Azure.DocumentDB 1.19.1、Newtonsoft.Json 9.0.1)

<小时/>
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;

class Program
{
static string endpoint = ConfigurationManager.AppSettings["endpoint"];
static string key = ConfigurationManager.AppSettings["key"];
static string database = ConfigurationManager.AppSettings["database"];
static string collection = ConfigurationManager.AppSettings["collection"];
static void Main(string[] args)
{
ConnectionPolicy cp = new ConnectionPolicy()
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Https,
RetryOptions = new RetryOptions()
{
MaxRetryAttemptsOnThrottledRequests = 5,
MaxRetryWaitTimeInSeconds = 60,
}
};

var client = new DocumentClient(new Uri(endpoint), key, cp);
//Step1 Create Database
client.CreateDatabaseIfNotExistsAsync(new Database { Id = database }).Wait();

//Step2 Create Collection(400RU)
client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(database),
new DocumentCollection
{
Id = collection,
PartitionKey = new PartitionKeyDefinition()
{ Paths = new Collection<string>() { "/partitionKey" } }
}
, new RequestOptions { OfferThroughput = 400 }).Wait();

//Step3 Insert TestData
var id1 = Guid.NewGuid().ToString();
client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(database, collection)
, new TestDocument() { TestField = "initialvalue", PartitionKey = "default", Id = id1, }).Wait();
var id2 = Guid.NewGuid().ToString();
client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(database, collection)
, new TestDocument() { TestField = "initialvalue", PartitionKey = "default", Id = id2, }).Wait();

//Step4 ID1 GetDocument(ReadDocument)
var readDocument = client.ReadDocumentAsync<TestDocument>(UriFactory.CreateDocumentUri(database, collection, id1),
new RequestOptions() { PartitionKey = new PartitionKey("default") }).Result.Document;

//Step5 ID2 GetDocument(DocumentQuery)
var queryDocument = client.CreateDocumentQuery<TestDocument>(UriFactory.CreateDocumentCollectionUri(database, collection),
new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = false, })
.Where(x => x.PartitionKey == "default" && x.Id == id2)
.AsDocumentQuery().ExecuteNextAsync<TestDocument>().Result.FirstOrDefault();

//Step6 ChangeValue
readDocument.TestField = "newvalue";
queryDocument.TestField = "newvalue";

//Step7 ID1 Replace
var updateResult1 = client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(database, collection, readDocument.Id), readDocument).Result;
//Step8 ID2 Replace
var updateResult2 = client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(database, collection, queryDocument.Id), queryDocument).Result;

//Step9 ID1 select
var id1Updated = client.ReadDocumentAsync<TestDocument>(UriFactory.CreateDocumentUri(database, collection, id1),
new RequestOptions() { PartitionKey = new PartitionKey("default") }).Result.Document;
//Step10 ID2 select
var id2Updated = client.ReadDocumentAsync<TestDocument>(UriFactory.CreateDocumentUri(database, collection, id2),
new RequestOptions() { PartitionKey = new PartitionKey("default") }).Result.Document;

Console.WriteLine(id1Updated.TestField); //newvalue
Console.WriteLine(id2Updated.TestField); //initialvalue
Console.ReadLine();

//Step11 ID2 SetPropertyValue
queryDocument.SetPropertyValue("testField", "newvalue2");
//Step12 ID2 Replace
var updateResultX = client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(database, collection, queryDocument.Id), queryDocument).Result;
//Step13 ID2 select
var id2UpdatedX = client.ReadDocumentAsync<TestDocument>(UriFactory.CreateDocumentUri(database, collection, id2),
new RequestOptions() { PartitionKey = new PartitionKey("default") }).Result.Document;
Console.WriteLine(id2UpdatedX.TestField); //newvalue2
Console.ReadLine();
}
}
public class TestDocument : Document
{
[JsonProperty("partitionKey")]
public string PartitionKey { get; set; }
[JsonProperty("testField")]
public string TestField { get; set; }
}

最佳答案

What is the difference between ReadDocumentAsync and CreateDocumentQuery when replacing?

您应该在查询上使用 ReadDocumentAsync 来进行简单的 ID 查找。

在服务器端,ReadDocumentAsync(REST API 上的 GET)在吞吐量 (RU) 和延迟方面更加轻量级。没有 SQL 解析/编译/索引扫描,只是直接查找。

ReadDocumentAsync 当找不到 here 中提到的特定文档(在状态代码中返回 404)时,将抛出 DocumentClientException。 .

要避免处理此异常,您可以首先使用 CreateDocumentQuery() 进行查询而不是直接读取。然后,您将获得一个结果集。

Is it appropriate to use the value obtained by DocumentQuery for replacement?

是的,您可以使用DocumentQuery获得的文档进行替换操作。请引用下面的代码片段:

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("db", "coll"))
.Where(r => r.Id == "**")
.AsEnumerable()
.SingleOrDefault();

MyMode mode= (dynamic)doc;

mode.name = "updated value";

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


Console.WriteLine(updateddoc);
}

public class MyMode
{
public string id { get; set; }
public string name { get; set; }
}

希望对您有帮助。

关于.net - [DocumentDB .NET SDK]替换时ReadDocumentAsync和CreateDocumentQuery有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48456744/

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