c# – 尝试从Cosmos DB中删除时未找到资源

当我尝试删除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 say PartitionKey you
imply a value like an int or string e.g 99 above.

Whereas when you say PartitionkeyPath you imply the property
path/name in the document e.g /CountryId in above.

参考文献IDocumentClientDocumentClient

点赞