Getting Started With CosmosDB

DocumentDB is a NoSQL database which is massively scalable and it works with schema-free JSON documents.

Features of CosmosDB

  • Schema-free JSON
    • Documents are stored in the form of JSON
    • Documents are easily queryable using SQL and number of APIs
  • ACID transactions
    • Allows multi-document transaction processing
  • Tunable Performance :

There are many ways to improve the performance

  • Throughput: Can easily scale throughput (request units processing per second)
  • Indexing: Can customize indexing policy as per requirement
  • Consistency: Supports multiple consistency policies.
  • Runs on Azure
    • Fully managed PaaS and massively scalable service
  • Multiple data models and API for accessing and querying the data

Query data using the following APIs

DocumentDB Resource Model

CosmosDB

  • Database Account
    • A database account is associated with a set of databases
  • Database
    • Database is logic container of documents
    • Database can have users associated with it
    • Users can have permissions for accessing the database
  • Collections
    • Collection is container of JSON documents
    • You can write Stored Procedures, Triggers and User-defined functions on all documents within the collection.
    • We use Javascript to write Stored Procedures, Triggers, and User-defined functions.
  • Document
    • User-defined JSON content. No schema needs to be defined.
    • A document can have attachments containing references and associated metadata for external blob/media like videos, images etc.

Create a database account

  1. In a new browser window, sign in to the Azure portal.
  2. Click on "More Service" and search for ‘Azure Cosmos DB’.

    CosmosDB

  3. Enter unique database name, select API as SQL, and select Subscription.

    CosmosDB

Creating new Collection

  • Click on Data Explorer and click New Collection.

    CosmosDB
  • Database Id: Enter new Database Id or select an existing Database
  • Collection Id: Give unique collection name

Add Sample Data in to Collection

  1. Click ‘Documents’ option under collection (dbFamily -> Families -> Documents).
  2. Click New Document.

    CosmosDB

  3. Add the following JSON data of family.

    CosmosDB

  4. Click on "Save and Observe the data".

System generated properties

Once you save the data, you can observe some system generated properties, prefixed with underscore (_).

CosmosDB

  • Id
    • User-defined unique name of the resource (with the same partition key value).
    • If the user does not specify an id, system will generate GUID
  • _rid
    • System generated, unique and hierarchical identifier of the resource
  • _etag
    • etag of the resource required for optimistic concurrency control
  • _ts
    • Last updated timestamp of the resource
  • _self
    • Unique addressable URI of the resource