0
votes

I have an Azure CosmosDB SQP API account with one container "EmployeeContainer", with the partition key "personId". I have three different type of collections in this container. Their schema are as shown below:

Person Collection:

{
    "Id": "1234569",
    "personId" : "P1241234",
    "FirstName": "The first name",
    "LarstName": "The last name"
}

Person-Department Collection:

{
    "Id": "923456757",
    "personId" : "P1241234",
    "departmentId": "dept01",
    "unitId": "unit01",
    "dateOfJoining": "joining date"
}

Department-Employees

{
    "id": "678678",
    "departmentId" : "dept01",
    "departmentName": "IT",
    "employees" : [
        { "personId": "P1241234" },
        { "personId": "P1241235" },
        { "personId": "P1241236" },
    ]   
}

How will the data be stored in the logical partitions? PersonId is the partition key and all the collections have personId in it. So, the document in the person collection with the person id "P1241234" and the document in the Person-Department collection with person id "P1241234" will be stored in the same logical partition? How will be the data in the Department-Employees be stored?

2

2 Answers

0
votes

Yes, that is true, documents with the same personId will be stored under the same logical partition (regardless of their type\schema). I'm not sure you can create documents without the partition key on a collection with a partition key, but if you can - all of them should be under the same logical partition (but I dont think you can create them).

1
votes

This design is not optimal. You should combine Person and Person-Department into a single collection using personId as the partition key, then have a second container for departments that has departmentId as it's partition key with each person as another row and any other properties that you would need when querying that collection. Do not write code that queries both containers. Each should have all the data it needs to satisfy any request you make. Then use change feed to keep them in sync.

You can get more details on how to model this article here