I have a method in my code which is responsible for inserting single documents into cosmosDB. And it used to work fine when i used to insert document without specifying partitionId in RequestOptions:
public Document createOne(JSONObject aJSONObject) throws DocumentClientException {
Document aDocument = new Document(aJSONObject.toString());
return documentClient.createDocument(getCollectionLink(), aDocument, null, false).getResource();
}
But if i change my code to pass partitionId in RequestOptions
like this:
public Document createOne(JSONObject aJSONObject, String aPartitionKeyStr) throws DocumentClientException {
Document aDocument = new Document(aJSONObject.toString());
PartitionKey aPartitionKey = new PartitionKey(aPartitionKeyStr);
RequestOptions requestOptions = new RequestOptions();
requestOptions.setPartitionKey(aPartitionKey);
return documentClient.createDocument(getCollectionLink(), aDocument, requestOptions, false).getResource();
}
I get the above error: PartitionKey extracted from document doesn't match the one specified in the header
. I'm pretty sure i'm passing the partition key value correctly as i have the same createMany
method which uses a stored procedure for bulk insertion and passes partitionKey in requestOptions, and it's working fine. I've looked up on internet and found similar questions that seemed to be related to my problem like this and this, but they focus on passing the partitionKey correctly. I'm sure i'm passing the partitionKey correctly.
In our collection, /clientId
key is used as a partition key, setting the breakpoint at the first line of the insertion function gives this output, and aPartitionKey is printed as follows: ["shiv-postman-client"]
I have one more doubt, would passing partitionKey while inserting a single document would make any improvements in performance? I saw in azure's sdk documentation also here(the example that shows the partitioned collection usage), that they haven't used partition key while insertion, only during querying.