You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

删除Cosmos DB数据时遇分区键错误,求正确删除方案

解决Cosmos DB删除文档时的"Entity with the specified id does not exist"错误

问题根源

你的Cosmos DB容器使用了分区键/content.provider.id,但删除操作时指定了PartitionKey = new PartitionKey(Undefined.Value),这会导致Cosmos DB无法定位到文档所在的分区——即使文档实际存在,也会返回"不存在"的错误。分区容器中执行删除/写入操作必须明确指定目标文档的分区键值。

修正方案

1. 提取正确的分区键值

从已读取的Document对象中直接提取分区键值,有两种便捷方式:

  • 方式一:使用Document自带的属性获取方法逐层取值:
    var providerId = document.GetPropertyValue<JObject>("content")?["provider"]?["id"]?.ToString();
    
  • 方式二:通过反序列化后的对象获取(若有对应实体类可直接强转,无实体类用JObject即可):
    var jsonObj = JsonConvert.DeserializeObject<JObject>(document.ToString());
    var providerId = jsonObj["content"]["provider"]["id"].ToString();
    

2. 修正删除操作代码

在DeleteDocumentAsync的RequestOptions中传入正确的分区键值,替换原来的Undefined.Value。

完整修正后的代码片段

foreach (Document document in item)
{
    string blobId = document.Id;
    // 直接复用Document的原始Json内容,避免重复序列化反序列化
    byte[] contentBytes = Encoding.UTF8.GetBytes(document.ToString());
    using (Stream ms = new MemoryStream(contentBytes))
    {
        try
        {
            // 上传Blob到存储容器
            await ConnectionFactory.GetConnectionBlob(DestinationStorageContainerName, storageConnectionString)
                .GetBlobClient(blobId).UploadAsync(ms, true);

            // 提取分区键值
            var providerId = document.GetPropertyValue<JObject>("content")?["provider"]?["id"]?.ToString();
            if (string.IsNullOrEmpty(providerId))
            {
                Console.WriteLine($"文档{document.Id}缺少分区键值,跳过删除");
                count++;
                continue;
            }

            // 执行删除操作,传入正确的分区键
            await ConnectionFactory.GetConnectionDocumentClient(cosmosAccount, cosmosKey)
                .DeleteDocumentAsync(
                    UriFactory.CreateDocumentUri(SourceCosmosDatabaseName, SourceCosmosCollectionName, document.Id),
                    new RequestOptions { PartitionKey = new PartitionKey(providerId) });

            Console.WriteLine($"文件已复制并删除 - 计数: {count}");
            count++;
        }
        catch (CosmosException ce)
        {
            Console.WriteLine($"删除失败: {ce.Message},文档ID: {document.Id}");
            Console.WriteLine($"已复制文件计数: {count}");
            count++;
        }
    }
}

额外优化点

  • 避免重复序列化/反序列化:直接使用document.ToString()获取原始Json内容,无需多次转换浪费性能。
  • 增加分区键值空值判断,避免因数据缺失导致的额外异常。

内容的提问来源于stack exchange,提问作者vish1990

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 23:03:32