- DocumentDB - Visualize Data
- DocumentDB - Access Control
- DocumentDB - Data Migration
- DocumentDB - Partitioning
- DocumentDB - Geospatial Data
- DocumentDB - Indexing Records
- DocumentDB - Sorting Records
- DocumentDB - Limiting Records
- DocumentDB - Data Types
- DocumentDB - Data Modeling
- DocumentDB - Delete Document
- DocumentDB - Update Document
- DocumentDB - Query Document
- DocumentDB - Insert Document
- DocumentDB - Delete Collection
- DocumentDB - Create Collection
- DocumentDB - Drop Databases
- DocumentDB - List Databases
- DocumentDB - Create Database
- DocumentDB - Connect Account
- DocumentDB - Create Account
- DocumentDB - Environment Setup
- DocumentDB - Advantages
- DocumentDB - Introduction
- DocumentDB - Home
DocumentDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
DocumentDB - Create Database
In this chapter, we will learn how to create a database. To use Microsoft Azure DocumentDB, you must have a DocumentDB account, a database, a collection, and documents. We already have a DocumentDB account, now to create database we have two options −
Microsoft Azure Portal or
.Net SDK
Create a Database for DocumentDB using the Microsoft Azure Portal
To create a database using portal, following are the steps.
Step 1 − Login to Azure portal and you will see the dashboard.
Step 2 − Now cpck on the created DocumentDB account and you will see the details as shown in the following screenshot.
Step 3 − Select the Add Database option and provide the ID for your database.
Step 4 − Cpck OK.
You can see that the database is added. At the moment, it has no collection, but we can add collections later which are the containers that will store our JSON documents. Notice that it has both an ID and a Resource ID.
Create a Database for DocumentDB Using .Net SDK
To create a database using .Net SDK, following are the steps.
Step 1 − Open the Console Apppcation in Visual Studio from the last chapter.
Step 2 − Create the new database by creating a new database object. To create a new database, we only need to assign the Id property, which we are setting to “mynewdb” in a CreateDatabase task.
private async static Task CreateDatabase(DocumentCpent cpent) { Console.WriteLine(); Console.WriteLine("******** Create Database *******"); var databaseDefinition = new Database { Id = "mynewdb" }; var result = await cpent.CreateDatabaseAsync(databaseDefinition); var database = result.Resource; Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId); Console.WriteLine("******** Database Created *******"); }
Step 3 − Now pass this databaseDefinition on to CreateDatabaseAsync, and get back a result with a Resource property. All the create object methods return a Resource property that describes the item that was created, which is a database in this case.
We get the new database object from the Resource property and it is displayed on the Console along with the Resource ID that DocumentDB assigned to it.
Step 4 − Now call CreateDatabase task from the CreateDocumentCpent task after DocumentCpent is instantiated.
using (var cpent = new DocumentCpent(new Uri(EndpointUrl), AuthorizationKey)) { await CreateDatabase(cpent); }
Following is the complete Program.cs file so far.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Azure.Documents; using Microsoft.Azure.Documents.Cpent; using Microsoft.Azure.Documents.Linq; using Newtonsoft.Json; namespace DocumentDBDemo { class Program { private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/"; private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/ StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ=="; static void Main(string[] args) { try { CreateDocumentCpent().Wait(); } catch (Exception e) { Exception baseException = e.GetBaseException(); Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message); } Console.ReadKey(); } private static async Task CreateDocumentCpent() { // Create a new instance of the DocumentCpent using (var cpent = new DocumentCpent(new Uri(EndpointUrl), AuthorizationKey)) { await CreateDatabase(cpent); } } private async static Task CreateDatabase(DocumentCpent cpent) { Console.WriteLine(); Console.WriteLine("******** Create Database *******"); var databaseDefinition = new Database { Id = "mynewdb" }; var result = await cpent.CreateDatabaseAsync(databaseDefinition); var database = result.Resource; Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId); Console.WriteLine("******** Database Created *******"); } } }
When the above code is compiled and executed, you will receive the following output which contains the Database and Resources IDs.
******** Create Database ******* Database Id: mynewdb; Rid: ltpJAA== ******** Database Created *******Advertisements