Franky Gupta
Franky Gupta

Reputation: 11

How to define & set partition key when doing a bulk operation on Document DB via a stored procedure

I am writing to my document DB via stored proc. This stored proc helps me do bulk operations, with lists that are dynamically sized as per my batch size which is decided dynamically depending on the user input. I have been trying to figure out if I should be adding partition keys to my collections and If yes, how? I have read over and found out that this is the way of doing it:

await client.ExecuteStoredProcedureAsync<DeviceReading>(
    UriFactory.CreateStoredProcedureUri("db", "coll", "SetLatestStateAcrossReadings"),
    new RequestOptions { PartitionKey = new PartitionKey("XMS-001") }, 
    "XMS-001-FE24C");

But this makes the partition key static, whereas my code is something like this:

await documentClient.ExecuteStoredProcedureAsync<int>(storedProcedureUri, new dynamic[] { documents });
where documents is a List<DocumentDbDocument>.

Upvotes: 0

Views: 524

Answers (1)

Michael Koltachev
Michael Koltachev

Reputation: 509

It's not clear whether your collection is partitioned. For partitioned collection, a sproc can currently operate only on documents that have same PK value as one in RequestOptions that you pass to ExecuteStoredProcedureAsync. This means, in particular, that all documents in the list must have same value of partition key.

If you want to use sproc for bulk insert, you would need to group your documents by value of partition key and send each group to the sproc. Alternatively you can use CreateDocumentAsync from client. When using multiple concurrent tasks that would provide similar throughput as bulk insert using sproc.

Upvotes: 1

Related Questions