Document DB is Microsoft’s flavor for NoSQL document database offered by Cosmos DB. In this blog, we are going to set up our very first DocumentDB database.
Step 1: Get an Azure Account
It’s easy, it’s free. You can sign up with an initial credit of $100 (you must provide credit card information but they don’t charge you without consent). Just go to www.azure.com and sign up if you don’t have an account already. Once you’ve signed up or signed in to the Azure Portal, you can browse through the list of Azure services and select the Azure Cosmos DB option.
Step 2: Create a new Azure Cosmos DB
After selecting the Azure Cosmos DB option, you must create a new Cosmos DB account by clicking Add. You will see a screen that allows you to enter the details of the Cosmos DB account. Enter ID, API, Subscription, Resource Group, and Location. You can select which Azure region your account will be hosted on. The ID is a unique global identifier within Microsoft Azure for the Cosmos DB account. Make sure to select SQL (DocumentDB) as the API. To finalize the creation of the account, click Create.
Once the Cosmos DB account has been created, it can be seen as follows.
Step 3: Create a new Collection
In Cosmos DB data is stored in collections. Choose ‘.Net’ platform and click ‘Create ‘Items’ collection’. Once the Items collection is created, you’ll see following screen.
Switch to Data Explorer (Preview) and let’s add new a new collection for ‘Customers’.
By default, ToDoList database and Items Collection was added. If we have to compare SQL server with DocumentDB then, Cosmos Account will be your SQL server, ToDoList will be your database, and Items Collection will be your table and rows will be items inside a collection.
Step 4: Add Documents
Now that we have a collection which is like a table in SQL, lets add documents to our database. Documents can be compared to rows in SQL table. Select Document option inside a collection and click ‘New Document’. Remove default JSON and enter your custom JSON.
After you save the new JSON document, notice how a new property called id is automatically added. If you don’t provide a unique id, DocumentDB will automatically create one using a GUID. Keep repeating the process to add more documents.
Manually entering each document into DocumentDB can be a tedious task and thus there are Data Migration Tools that imports data from DQL and other data sources. There is an open source project called DocumentDB Data Migration Tool that does this.
Querying Using Document DB GUI
To get started, click Data Explorer (Preview), select the Items collection and click the ellipsis (…) button. A small menu will appear with a few options. Then click the New SQL Query option. This will open a query tab, enter your query and click ‘Execute Query’. Here is the query result.
Notice that the resulting document contains additional properties that were not originally visible when the document was added to the collection. These properties are the document’s resource ID (_rid); time stamp (_ts), which represents a raw integer value for when the document was last inserted or updated; self-link (_self), which drills down on _rid from the database to the collection and to this specific document; _etag, used for concurrency; and the _attachments link (in case there are any).
JOIN in DBDocument
Let’s do a basic query with JOIN. A very important distinction between SQL and DocumentDB is that DocumentDB JOIN supports intra-document joins and not inter-document joins. In SQL we can join 2 tables but we cannot join 2 documents in Document DB. For the document below, we can use join to get address details.