1
votes

I am working on Bulk insert stored procedure in Cosmos database using document client but the challenge that I am facing is, I need to insert documents in bulk that may have different partition keys.

Is there any way to achieve it?

I am currently using the below code:

  Uri uri = UriFactory.CreateStoredProcedureUri("test-db", "test-collection", "sp_bulk_insert");
  RequestOptions options = new RequestOptions { PartitionKey = new PartitionKey("patient")};
  var result = await _client.ExecuteStoredProcedureAsync<string>(uri, options , patientInfo, pageInfo);
  return result;

But I also have pageInfo object having partition key: "page" but given PartitionKey in RequestOptions is "patient" that is the partition key of patientInfo object

When I am trying to execute the SP it is giving following error:

Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted
1
are you using the bulk executor library as that can do the splitting of partitions ?Mark West

1 Answers

3
votes

Stored procedures are scoped to a single partition key so this not possible. Also there is no reason to use stored procedures for bulk operations. You are better off using the .NET SDK v3 and leveraging the bulk support in there. https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/BulkSupport

Thanks.