4

This is how my document looks, I wanted to choose the partition key as /department/city; which is two different attribute, one form employee and another from address(which is an embedded object to employee). I tried give /department/address as partition key but it's not listing the partition key in data-explorer, I'm assuming it considering city is a attribute in department.

{
  "eid": "",
  "entryType": "",
  "address": 
  {
    "PIN": "",
    "city": "",
    "street": ""
  },
  "name": "",
  "id": "",
  "department": "",
  "age":
}

Can you please help me understand what I'm doing wrong and how to design a composite partition key and distribute/store/arrange employees data based on their department and city.

2 Answers 2

3

Can you please help me understand what I'm doing wrong and how to design a composite partition key and distribute/store/arrange employees data based on their department and city.

If I am not mistaken, currently composite partition keys are not supported. In a collection you must define a partition key using just one attribute.

However if you look at the REST API, the partition key is defined as an array (albeit that array only contains 1 element). This tells me that Azure may support composite partition keys in future.

So, for now you pick one attribute (either department or city) to partition the data and define an index on the other attribute for faster searching.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @gaurav, yes I can go with single field partition key but the problem is there in no straightforward way to change the partition key if I wish in future.
That's correct. You would really need to think about partitioning strategy at the time of collection creation.
Just thinking out loud, you could create an object type attribute with department & city and set that as partition key.
yes @gaurav I was thinking the same, but taking the queribility into account I believe it wouldn't be a great option; especially while scanning the documents.
@GauravMantri is the composite key supported now with document db. Currently I am using this SDK : implementation group: 'com.microsoft.azure', name: 'azure-documentdb-rx', version: '0.9.0-rc2'
1

In my CosmosDb multi-partitioned collections I generally specify that the partitionKey should be generic and just use a property that is literally called "partitionKey". The advantage of this is that you have much more fine grained control over how your data is stored by simply specifying a value that makes sense for the particular POCO you are inserting.

There's a high likelihood that somewhere down the line you're going to want to insert a document into this collection that doesn't conform to the structure of the document you showed us here. At this point you'll either have to split your docs across multiple collections or redesign your whole partition key strategy.

The trade off here is that during querying you have to have some knowledge of how these values are applied but this can easily be enforced in code through a lightweight ORM layer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.