Looking at how to set up DocumentDb with a partition key, if I have a multi-tenant application, where most documents have a tenantId, it seems reasonable to create the collection with a partition key of /tenantId/.
What happens to documents that don't have a tenantId property? Will they just get added without one, or will Cosmos DB create those objects/documents with some kind of system (hard-coded) tenantId, so they are stored together?
It isn't mandatory to explicitly tell cosmos about the partition key (via argument or CosmosItemRequestOptions) during a CRUD operation. If unavailable, cosmos reads the document (which is being added/updated) and finds the partition key. Sending partition key argument as null is same as not sending partition key.
When you save a document that's missing the property specified by your partition key, it will result in assigning an "undefined" value for its partition key. In the future, if you wanted to provide a value for the partition key of such a document, you'd have to delete, and then re-add, the document.
Most small containers in Azure Cosmos DB only require one or two physical partitions. If your container could grow to more than a few physical partitions, then you should make sure you pick a partition key that minimizes cross-partition queries.
Azure Cosmos DB is highly scalable And that is because it uses a partitioning system to scale, which consists of physical and logical partitions. To optimize the scalability and performance of Azure Cosmos DB, you need to choose the right partition key for your container.
These documents are automatically assigned the "undefined" value for partition key (similar to JavaScript's undefined). This is treated like any other value, and all documents with missing keys are stored within the same partition. If you have a large number of these, then you would have trouble scaling as all writes within missing keys would get assigned to the same partition.
It's generally a good idea to avoid this, or fix this later by deleting/reinserting (if this lack of partition key is due to some asynchronous nature of not knowing the partition key at creation time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With