I set the partition key of one of my Cosmos databases to /partition .
For example: we have a Chat document containing a list of subscribers, then we have ChatMessages , which contains text, a link to the author, and some other properties. Both documents have a partition property, which contains the type "chat" and a chat identifier.
Chat example:
{ "id" : "955f3eca-d28d-4f83-976a-f5ff26d0cf2c", "name" : "SO questions", "isChat" : true, "partition" : "chat_955f3eca-d28d-4f83-976a-f5ff26d0cf2c", "subscribers" : [ ... ] }
Then we have Message docs:
{ "id" : "4d1c7b8c-bf89-47e0-83e1-a8cf0d71ce5a", "authorId" : "some guid", "isMessage" : true, "partition" : "chat_955f3eca-d28d-4f83-976a-f5ff26d0cf2c", "text" : "What should I do?" }
Now it’s very convenient to return all messages for a specific chat, I just need to request all the documents in the chat_955f3eca-d28d-4f83-976a-f5ff26d0cf2c using the isMessage = true property. Things are good...
But if now I want to request my db for a specific message by id, I usually just know the identifier, but not the section, and therefore should run a slow crosspartition request. Which then led me to the question if I shouldn't add partitionKey to the message id so that I could split the id when requesting db for a faster search. I saw that the _rid property of the document looks like a combination of the db id and collection id, and then the document id. I mean this (simplified):
Chat.Id = "abc" Chat.Partition = "chat_abc" //[type]_[chatId] Message.Id = "chat_abc|123" //[Chat.Partition]|[Message.Id] Message.Partition = chat_abc //[Chat.Partition]
Suppose now that I want to get a Message document using id , I just split id by character | , and then requested a document with the 1st part of id as a section and a full identifier as a key.
It makes sense? Are there any better ways to do this? Should I just always pass the partitionKey document together, not just id ? Should I just use the _rid properties?
Any experience is greatly appreciated!
UPDATE
I found the following answer here :
Some applications encode a section key as part of an identifier, for example. the section key will be the customer’s identifier, and ID = "customer_id.order_id", so you can extract the section key from the identifier value.
I also asked the space team via email if this is the recommended sample and send a response if I receive it.