Error when bulk inserting documents with Cosmos DB (Response status code does not indicate success: Conflict (409); Substatus: 0; ...)

Syifa Prasetyo 20 Reputation points
2024-04-05T08:08:38.3866667+00:00

I am trying to add a collection of this object:


public class Organization
{
	[JsonProperty("id")]
	public string? Id { get; set; }
	[JsonProperty("name")]
	public string? Name { get; set; }
	[JsonProperty("address")]
	public string? Address { get; set; }
	[JsonProperty("createdOn")]
	public DateTime CreatedOn { get; set; }
	[JsonProperty("updatedOn")]
	public DateTime UpdatedOn { get; set; }
	[JsonProperty("owner")]
	public User? Owner { get; set; }
	[JsonProperty("creator")]
	public User? Creator { get; set; }
	[JsonProperty("lastUpdatedBy")]
	public User? LastUpdatedBy { get; set; }
	[JsonProperty("tenantId")]
	public string? TenantId { get; set; }
}

This is my code to bulk insert the documents:


public async Task<BulkOperationResponse<Organization>> BulkCreateOrganizationDB(List<Organization> reqs)
{
	BulkOperations<Organization> bulkOperations = new();

	foreach (var req in reqs)
	{
		PartitionKey partitionKey = new(req.TenantId);
		bulkOperations.Tasks.Add(_container.CreateItemAsync(req, partitionKey));
	}

	BulkOperationResponse<Organization> bulkResponse = await bulkOperations.ExecuteAsync();

	return bulkResponse;
}

But I get the following error that does not provide much information

Response status code does not indicate success: Conflict (409); Substatus: 0; ActivityId: 1cc2c968-2ccd-4a8d-8207-1a9b71b89d33; Reason: ();

I am using tenantId as the Partition Key in Cosmos DB..

I also tried the same approach for bulk update, and it worked correctly. How can I resolve this issue with bulk insert?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,520 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rico Wang 75 Reputation points Microsoft Employee
    2024-06-13T09:36:39.3833333+00:00

    Hi Syifa,

    For the status code, see our documentation at 409 Conflict.

    The ID provided for a resource on a PUT or POST operation has been taken by an existing resource. Use another ID for the resource to resolve this issue. For partitioned collections, ID must be unique within all documents with the same partition key value.

    How was the ID field of your documents generated?

    0 comments No comments