Divya Agrawal
Divya Agrawal

Reputation: 285

DocumentDB: Bulk-Import Stored Procedure: Insert multiple partition key documents in COSMOS DB

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

Upvotes: 2

Views: 780

Answers (1)

Mark Brown
Mark Brown

Reputation: 8793

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.

Upvotes: 3

Related Questions