Response status code does not indicate success when inserting documents

Viewed 10756

Have have following partion id on my container: /vesselId

I am trying to add a collection of this object:

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

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

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

This is my code to bulk insert the documents:

 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);

I get following error that does not provide much information?

Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: BadRequest (400); Substatus: 1001; ActivityId: ; Reason: ();'

Any pointers to what causes this? This is my settings: enter image description here

I have same problem when deleting documents:

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));

Throws following error: 'Response status code does not indicate success: NotFound (404); Substatus: 0;

3 Answers

I'm from the CosmosDB Engineering Team. From your question, you've defined the partition key on the container as /vesselId, whereas the document has mapped the vesselId to the "id" property in the CoachVessel class. Is this intentional?

If the optional PartitionKey is specified in the CreateItemAsync API, then it needs to match the partition key in the Document. If you intended "id" to be the partition key, then you need to define your container's partition key as "id", not "vesselId". In this case, if the container's partition key is indeed /vesselId, the code expects a property "vesselId" in the input document set to the value vessel.vesselId specified in the partition key. It looks like the "vesselId" property is missing in your input document.

The partition key must match a property in the document body. Change the partition key for the container to be, /id and fix your deletion code to correctly specify the partition key. E.g.,

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

In my case the below error was because I'd updated the .Net SDK from v2 to v3, which no longer auto-generates IDs if one isn't passed.

Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: BadRequest (400); Substatus: 1001; ActivityId: ; Reason: ();'

I use the repository pattern, so just added a check before calling CreateItemAsync:

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