【问题标题】:Response status code does not indicate success when inserting documents插入文档时响应状态码不表示成功
【发布时间】:2020-10-29 00:30:33
【问题描述】:

我的容器上有以下分区 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-cosmosdb azure-cosmosdb-sqlapi


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。我实际上是这样开始的,但从字面上尝试了所有选择。正如我们所说,我的分区键是/id。我已经尝试过 concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/id")));, concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/d")));,concurrentTasks。 Add(container.AddItemAsync(vessel, new PartitionKey("/vesselId")));和 concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("vesselId")));都给出相同的错误。如果我不提供分区密钥,我不会收到任何错误。
    • 顺便说一句,这是我的第一个 cosmos db 应用程序,我可能错过了一些非常明显的东西。
    • 你没有传递分区键的路径你传递的值。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      在我的情况下,以下错误是因为我已将 .Net SDK 从 v2 更新到 v3,如果 ID 未通过,它将不再自动生成 ID。

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

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

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

      【讨论】:

        猜你喜欢
        • 2016-04-22
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 2022-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多