当我尝试删除Cosmos DB的资源时,我收到以下错误:找不到资源.当我开始使用带有分区键的无限集合时,它就开始发生了.这没有partionkey和限制10gb集合工作正常.
protected async Task<bool> DeleteDocument(Resource document) { var documentUri = UriFactory.CreateDocumentUri(_db.Options.Value.DatabaseName,_db.Options.Value.CollectionName,document.Id); ResourceResponse<Document> result = null; var options = new RequestOptions { PartitionKey = new PartitionKey("moachingpartionkey") }; for (int i = 0; i < MaxRetryCount; i++) { try { result = await _db.Client.DeleteDocumentAsync(documentUri,options); break; } catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429) { _logger.LogWarning($""); await Task.Delay(dex.RetryAfter); } } if (result == null) return false; int statusCode = (int)result.StatusCode; return statusCode >= 200 && statusCode < 300; }
这是我的创作:
protected async Task<bool> CreateDocumentAsync(Resource document) { var collectionUri = UriFactory.CreateDocumentCollectionUri(_db.Options.Value.DatabaseName,_db.Options.Value.CollectionName); ResourceResponse<Document> result = null; for (int i = 0; i < MaxRetryCount; i++) { try { result = await _db.Client.CreateDocumentAsync(collectionUri,document); break; } catch (DocumentClientException dex) when (dex.StatusCode.HasValue && (int)dex.StatusCode.Value == 429) { _logger.LogWarning($""); await Task.Delay(dex.RetryAfter); } } if (result == null) return false; int statusCode = (int)result.StatusCode; return statusCode >= 200 && statusCode < 300; }
解决方法
由于您在评论中询问过,这里是我用来在创建集合时添加分区键的代码:
var collection = new DocumentCollection { Id = "Customers",// just an example collection }; // Set partition key collection.PartitionKey.Paths.Add("/CountryId"); // just an example of the partition key path // Set throughput var options = new RequestOptions { OfferThroughput = 400,// Default value is 10000. Currently set to minimum value to keep costs low. }; // Create await client.CreateDocumentCollectionIfNotExistsAsync( UriFactory.CreateDatabaseUri("YourCosomosDatabaseId"),collection,options);
这是我用来删除文档的代码.请注意,我先检查它是否存在,否则我会得到例外.
// Check if it exists,otherwise delete throws var doc = await GetByIdAsync(id,99); // your method to fetch the document by Id,the partition key (CountryId) is 99 if (doc == null) { return true; // Indicates successful deletion } // Delete var uri = UriFactory.CreateDocumentUri("YourCosomosDatabaseId","Customers",id); var reqOptions = new RequestOptions { PartitionKey = new PartitionKey(99) }; // CountryId is 99 var result = await Client.DeleteDocumentAsync(uri,reqOptions); return result.StatusCode == HttpStatusCode.NoContent;
To clarify a bit of the terminology-
When you sayPartitionKey
you
imply a value like anint
orstring
e.g99
above.Whereas when you say
PartitionkeyPath
you imply the property
path/name in the document e.g/CountryId
in above.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。