Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to documents in documentDb that have no partition key property?

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?

like image 910
Mark Redman Avatar asked Oct 07 '17 06:10

Mark Redman


People also ask

Is partition key mandatory?

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.

What happens when you add a new property to a JSON document that doesn't exist in any other document when storing in Cosmos DB?

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.

Why do we need partition key in cosmos?

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.

Is partition key mandatory in Cosmos DB?

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.


1 Answers

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).

like image 173
Aravind Krishna R. Avatar answered Sep 28 '22 15:09

Aravind Krishna R.