首页 > 解决方案 > 插入文档时响应状态码不表示成功

问题描述

我的容器上有以下分区 ID:/vesselId

我正在尝试添加此对象的集合:

public class CoachVessel
{
    [JsonProperty("id")]
    public string vesselId { get; set; }

    [JsonProperty("imo")]
    public long Imo { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

这是我批量插入文档的代码:

 CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
 CosmosClient cosmosclient = new CosmosClient(connStr, options);
 Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");
    
 List<Task> concurrentTasks = new List<Task>();
 foreach (var vessel in vessels.Take(1))
 {
     concurrentTasks.Add(container.CreateItemAsync(vessel, new PartitionKey(vessel.vesselId)));
 }        
 await Task.WhenAll(concurrentTasks);

我收到以下没有提供太多信息的错误?

Microsoft.Azure.Cosmos.CosmosException: '响应状态码不表示成功:BadRequest (400); 子状态:1001;活动ID:;原因: ();'

任何指向是什么原因的指针?这是我的设置: 在此处输入图像描述

删除文档时遇到同样的问题:

CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true,  MaxRetryAttemptsOnRateLimitedRequests=1000};
CosmosClient cosmosclient = new CosmosClient(connStr, options);
Container container = cosmosclient.GetContainer("CoachAPI", "Vessels");


var allItemsQuery = container.GetItemQueryIterator<string>("SELECT * FROM c.id");

List<Task> concurrentDeleteTasks = new List<Task>();

while (allItemsQuery.HasMoreResults)
{
    foreach (var item in await allItemsQuery.ReadNextAsync())
    {
        concurrentDeleteTasks.Add(container.DeleteItemAsync<string>(item, new PartitionKey("id")));
    }
}
            
await Task.WhenAll(concurrentDeleteTasks.Take(3));

抛出以下错误:'响应状态码不表示成功:NotFound (404); 子状态:0;

标签: azure-cosmosdbazure-cosmosdb-sqlapi

解决方案


分区键必须与文档正文中的属性匹配。将容器的分区键更改为,/id并修复删除代码以正确指定分区键。例如,

concurrentDeleteTasks.Add(container.DeleteItemAsync<string>(item, new PartitionKey(item.Id)));

推荐阅读