8

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;

Thomas Segato
  • 4,567
  • 11
  • 55
  • 104

3 Answers3

3

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)));
Joel Oughton
  • 416
  • 3
  • 6
3

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();
}
SteveA
  • 456
  • 6
  • 14
1

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.

Krishnan Sundaram
  • 1,321
  • 9
  • 11
  • Thanks for your response. I actually started that way but literately tried all options. As we speak my partion key is /id. I have tried with concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/id")));, concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/d")));,concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("/vesselId"))); and concurrentTasks.Add(container.AddItemAsync(vessel, new PartitionKey("vesselId"))); All give same error. If I dont provide the partionkey i get no errors. – Thomas Segato Jul 09 '20 at 06:40
  • Btw notice this is my first cosmos db app I may have missed something very obvious. – Thomas Segato Jul 09 '20 at 06:45
  • 2
    You don't pass the path of the partition key you pass the value. – Mark Brown Jul 09 '20 at 20:49
  • I am trying to save document using Azxure Function and I get this error "Executed 'Functions.Function' (Failed, Id=6fe6c66d-07ee-41d9-af24-aa308aa9bcb4, Duration=2591ms) [2023-07-19T06:56:19.547Z] System.Private.CoreLib: Exception while executing function: Functions.Function. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5b313f49-3328-415e-9e5a-183a33940ad4; Reason: ({"Errors":["One of the specified inputs is invalid"]}" – redar ismail Jul 19 '23 at 06:57