How to query from two containers in Cosmos DB (SQL API)

Viewed 12044

I am new to cosmos db. I chose cosmos db (core sql), created a database having two containers say EmployeeContainer and DepartmentContainer. Now I want to query these two container and want to fetch employee details with associated department details. I stuck on a point and need help.

Below is the structure of my containers.
EmployeeContainer : ID, Name, DepartmentID
DepartmentContainer: ID, Name

Thanks in advance.

3 Answers

Cosmos DB is not a relational database. You do not store different entities in different containers if they are queried together. They are either embedded in other entities or stored as separate rows using a shared partition key with other entities in the same container.

Before you get too far with Cosmos you need to understand how to model and partition data to ensure the best possible performance. I strongly recommend you read the docs on partitioning and specifically read these docs below.

Data modeling in Cosmos DB

Partitioning in Cosmos DB

How to model and partition data - a real world example

And watch Data Modeling in Cosmos DB - What every relational developer should know

It completely depends on the type of data you are trying to model. Generally, it comes down to relationships. 1:1 or 1:few often are best for embedding related items or where queries are updated together. 1:many or many:many for referencing related items are queried or updated independently.

For great talks on these issues check out https://www.gotcosmos.com/conf/ondemand

Related