Azure DocumentDB is Microsoft’s new budding NoSQL Schema Free database offering which is relatively a recent addition to the mammoth Microsoft Data Platform Stack.

Azure DocumentDB provides great flexibility to the Developer world who feels a lot restricted with RDBMS data model, adhering to the data types, schema, index designs & patterns. Azure DocumentDB gives flexibility to the Developers to store entire records or JSON documents in Azure DocDB database uniquely identified by a key-value pair. All the attributes within a document are indexed by default with range or hash indexes thereby expediting the query processing against the documents & improving the query performance for reporting & analytics. Hence Developer just needs to serialize the data structures into JSON format & store them in Azure DocumentDB database while Azure DocumentDB takes care of indexing & storage of the attributes. Azure DocumentDB supports querying the attributes using the familiar SQL language over JSON structure making it compatible with most reporting & analytics platform.

While I started learning & evaluating Azure DocumentDB, I found one of component which was missing in Azure DocumentDB is out of box Powershell cmdlets for managing & administering the database. I found this was a opportunity for me to kick start a new community project in Github & try to get my hands dirty with Github public repository which I was aspiring to learn & adopt for quite sometime now.

With this excitement & modesty, I have created a project in Azure DocumentDB for Powershell cmdlets in Github to perform some basic management operation in Azure DocumentDB

https://github.com/savjani/Azure-DocumentDB-Powershell-Cmdlets

Further I invite the community members to further extend this modest list of Cmdlets to a full blown DocDB management & administration module similar to SQLPS

Powershell Cmdlet Definition & its Usage

Creating a New Database

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
$db = Add-Database -Context $ctx -Name “DocDBPS2”
Get-database -Context $ctx -SelfLink $db.SelfLink | ft

Creating a New Database & New Document Collection

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
$db = Add-Database -Context $ctx -Name “DocDBPS2”
$coll = Add-DocumentCollection -Context $ctx -DatabaseLink $db.SelfLink -Name “DocCollPS”
Get-database -Context $ctx -SelfLink $db.SelfLink | ft

List all the Databases in the Given Azure DocumentDB Database Account

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
Get-databases -Context $ctx | ft

List Database Properties for a given Database

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
$db = Get-databases -Context $ctx | Where-Object {$_.Id -eq “DocDBPS”}
Get-database -Context $ctx -SelfLink $db.SelfLink | ft

Drop Database

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
$db = Get-databases -Context $ctx | Where-Object {$_.Id -eq “DocDBPS”}
remove-database -Context $ctx -SelfLink $db.SelfLink

Get Database Account Consistency Level

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
Get-DatabaseAccountConsistencyLevel -Context $ctx

Set Database Account Consistency Level

Import-Module C:\Users\pariks\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll
$ctx = New-Context -Uri <uri> -Key <key>
Set-DatabaseAccountConsistencyLevel -Context $ctx -DefaultConsistencyLevel Eventual
Get-DatabaseAccountConsistencyLevel -Context $ctx

Hope this helps 🙂

Parikshit Savjani
Premier Field Engineer

Similar Posts

4 Comments

  1. Hi Parikshit,

    I need one help. Can you let me know that any powershell command through which we can create DocumentDB?

    Thanks,
    Birendra Goswami

  2. Hi Birendra,

    You can download the Module from GitHub link provided in this blog and use the Add-Database cmdlet to create a new DocDB database

  3. Hi Parikshit,

    I am using powershell version
    Major Minor Build Revisi
    —– —– —– ——
    5 0 10586 494

    I am running the below command
    “Import-Module C:\Users\arun_si\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll”
    It show below error.
    Import-Module : Could not load file or assembly ‘file:///C:\Users\arun_si\Documents\WindowsPowerShell\Modules\DocDB\Azrdocdb.dll’ or one of its dependencies. Operation is not supported.

    Can you pls give some suggestion to resolve this issue.

Leave a Reply to Arun Cancel reply

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