Supporting Parent/Child Documents in CosmosDB

In my efforts to break out of my relational database paradigm and into a NoSQL paradigm I have been working through some real world examples of database models that I have used in the past and trying different ways to convert them into a NoSQL database.

This is an example of three four different ways to store and access a document consisting of a parent and child relationship in a CosmosDB database.

The repository is located at https://github.com/mattruma/SampleCosmosDbParentChildPersistence.

For more information on data modeling in CosmosDB see https://docs.microsoft.com/en-us/azure/cosmos-db/modeling-data.

In most of the demo I provided examples of storing and retrieving the document with direct reads and writes to CosmosDB as well as an example that follows a more repository like the pattern, the latter allowing for better testing.

For an explanation of the repository pattern please see https://www.codecompiled.com/csharp/repository-pattern-in-csharp.

Azure CosmosDB Configuration

This code example assumes that you have an Azure account. If you don’t have an Azure account, create a free account at
https://azure.microsoft.com/en-us/free.

In your Azure account navigate to CosmosDB and create a single CosmosDB account, in my example I created a CosmosDB account called mjr-003-cosdb.

Create three four separate databases with the following collections:

You should set the partitionKey for all collections to orderId.

Application Settings Configuration

Add an appsettings.Development.json filed to each of the console applications and update your configuration to look like the below along with the CosmosDB Primary Connection String:

That’s it as far as setup goes!

Examples

Demo1ConsoleApp

This example demonstrates a single document that includes both the Order and the Order Line Items.

This is an example of an embedded data model, read more at https://docs.microsoft.com/en-us/azure/cosmos-db/modeling-data.

Executing this example inserts an Order document, which includes both the Order and related Order Line Items, into the orders collection.

Order Document

Demo2ConsoleApp

This example demonstrates inserting an Order document into an orders collection, and the related Order Line Item documents (see below) also inserted into the orders collection.

This is an example of a referenced data model, you can read more at https://docs.microsoft.com/en-us/azure/cosmos-db/modeling-data.

Order Document

Order Line Item Document

The type property allows you to determine what the document is, e.g. Order or Order Line Item.

The benefit of doing it this way is you can quickly retrieve all the documents, Order and related Order Line Items, with a single call using the partition key, which has been set to orderId.

Demo3ConsoleApp

This example demonstrates inserting an Order document into an orders collection, and the related Order Line Item documents inserted into an orderLineItems collection.

This is also an example of a referenced data model, except the different documents are stored in differenct collections, read more at https://docs.microsoft.com/en-us/azure/cosmos-db/modeling-data.

Order Document

Order Line Item Document

Demo4ConsoleApp

This example is similar to the Demo3ConsoleApp example, except Order and Order Line Item documents, which both implement the IOrderDocument interface, are fetched with a single call to CosmosDB and then converted to the correct document type using a custom JSON converter.

IOrderDocument interface implemented by both Order and Order Line Item

Custom JSON Converter to serialize based on document type, e.g. Order and Order Line Item

These are the most common ways I have found to persist parent and child related data in CosmosDB, there are probably other ways, maybe even better ways, if you have one, please share it with me.

Leave a Reply

Your email address will not be published. Required fields are marked *