Azure cosmosdb 插入文档时,响应状态代码不表示成功

Azure cosmosdb 插入文档时,响应状态代码不表示成功,azure-cosmosdb,azure-cosmosdb-sqlapi,Azure Cosmosdb,Azure Cosmosdb Sqlapi,我的容器上有以下分区id: /脉管 我正在尝试添加此对象的集合: public class CoachVessel { [JsonProperty("id")] public string vesselId { get; set; } [JsonProperty("imo")] public long Imo { get; set; } [JsonProperty("name")] p

我的容器上有以下分区id: /脉管

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

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);
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));
CosmosClientOptions=new CosmosClientOptions(){AllowBulkExecution=true};
CosmosClient CosmosClient=新的CosmosClient(connStr,选项);
Container Container=cosmoclient.GetContainer(“CoachAPI”、“Vessers”);
List concurrentTasks=新列表();
foreach(容器中的var容器。取(1))
{
Add(container.CreateItemAsync(容器,新分区键(容器.vesselId));
}        
等待任务。WhenAll(concurrentTasks);
我得到下面的错误,没有提供太多的信息

Microsoft.Azure.Cosmos.CosmosException:'响应状态代码不存在 表示成功:BadRequest(400);亚状态:1001;活动ID:; 理由:();'

有什么能说明这是什么原因吗?这是我的设置:

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

 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);
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));
CosmosClientOptions=new CosmosClientOptions(){AllowBulkExecution=true,MaxRetryAttemptsOnRateLimitedRequests=1000};
CosmosClient CosmosClient=新的CosmosClient(connStr,选项);
Container Container=cosmoclient.GetContainer(“CoachAPI”、“Vessers”);
var allItemsQuery=container.getItemQueryInterator(“从c.id中选择*);
List concurrentDeleteTasks=新列表();
while(allItemsQuery.HasMoreResults)
{
foreach(wait allItemsQuery.ReadNextAsync()中的变量项)
{
concurrentDeleteTasks.Add(container.DeleteItemAsync(item,new PartitionKey(“id”)));
}
}
等待任务.WhenAll(concurrentDeleteTasks.Take(3));
抛出以下错误:
'响应状态代码不表示成功:未找到(404);子状态:0

我是CosmosDB工程团队的。根据您的问题,您已经将容器上的分区键定义为/vesselId,而文档已经将vesselId映射到CoachVesser类中的“id”属性。这是故意的吗


如果在CreateItemAsync API中指定了可选的分区键,那么它需要与文档中的分区键匹配。如果希望“id”作为分区键,那么需要将容器的分区键定义为“id”,而不是“vesselId”。在这种情况下,如果容器的分区键确实是/vesselId,则代码希望输入文档中的属性“vesselId”设置为分区键中指定的值vessel.vesselId。输入文档中似乎缺少“vesselId”属性。

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

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

在我的例子中,下面的错误是因为我将.Net SDK从v2更新到了v3,如果没有通过,它将不再自动生成ID

Microsoft.Azure.Cosmos.CosmosException:'响应状态代码不存在 表示成功:BadRequest(400);亚状态:1001;活动ID:; 理由:();'

我使用存储库模式,因此在调用CreateItemAsync之前添加了一个检查:

if (item.Id == null)
{
    item.Id = Guid.NewGuid().ToString();
}

谢谢你的回复。事实上我是这样开始的,但我尝试了所有的选择。正如我们所说,我的分区密钥是/id。我已经尝试了concurrentTasks.Add(container.AddItemAsync(容器,新分区密钥(“/id”);,concurrentTasks.Add(container.AddItemAsync(容器,新分区键(“/d”));,concurrentTasks.Add(container.AddItemAsync(容器,新分区键(“/vesselId”));和concurrentTasks.Add(container.AddItemAsync(容器,新分区键(“vesselId”));都给出了相同的错误。如果我不提供PartitionKey,我不会出错。顺便说一句,这是我的第一个cosmos db应用程序,我可能错过了一些非常明显的东西。你没有传递分区键的路径,而是传递了值。