DocumentDB API

As we mentioned earlier, DocumentDB is Microsoft's own document-based NoSQL database. DocumentDB provides SQL query capabilities over schema-less document-based records.

Cosmos DB provides an API and SDKs in various languages to integrate DocumentDB with your application. Let's see a few advantages and features that DocumentDB API with Cosmos DB provides:

  • Elastic scale: Effectively scale up or downsize your database to meet your application needs. Your information is stored on solid state drives (SSD) for low latency. Cosmos DB stores the data in various collections, which can be scaled and used for a large set of records.
  • Replication: As you have learned in the last chapter, Cosmos DB databases, such as DocumentDB can be replicated across any number of regions supported in the Azure data center. This provides us the ability to develop globally-available applications with as low latency as possible. Cosmos DB also provides failover handling whether it's automatic or manual as discussed in Chapter 2, Cosmos DB Global Turnkey Distribution.
  • SQL-like query: DocumentDB is a NoSQL database, but with Cosmos DB, you can query those JSON documents using SQL-like queries. You can query your data at a very high speed without the need for specifying the indexes, views, and so on as we do in SQL databases.
  • Multiple consistency levels: You can select the consistency among the top five well-defined consistency levels. The consistency levels are strong, bounded staleness, session, consistent prefix, and eventual. This allows you the flexibility to choose and trade-off the performance and data consistency. In some applications, there is no need for a strict write but data needs to be shown in real time. In other applications, there is a need for strict write in the database. You can trade-off and choose the consistency levels. We will discuss more about the consistency levels in the coming sections.
  • Complete document indexing: Cosmos DB, by default, for performance sake, indexes all the documents in the database. You can, however, specify to provide an index on which document or key if you want, but this is the default behavior.
  • Real-time change feed: You can listen to every change in the database and get the notification. You can use this change feed to initiate the API call, do replication, or build an audit log.

Let's go ahead and create a DocumentDB database:

  1. Log in to your Azure portal and click on the Azure Cosmos DB option on the left side of the Option bar.
  2. Click on the + button and give your new database a proper ID and choose the SQL option from the API dropdown.

You can create a new resource or use an existing one. Select the location where the most writing will happen and click on the Create button. You can refer to the following screenshot for further information:

Azure will create the new database with DocumentDB API and notify you. You will get a notification similar to this:

Let's create a new collection and store some data in it:

  1. Go to Data Explorer on the left menu and click on the New Collection button.
  2. Give your database and collection a name and click on the OK button. Refer to the following screenshot for further information:
  1. Once the collection is created, we can create new documents in it. Click on the New document button and add the following JSON object:
{ 
    "id": "10", 
    "name": "Shahid", 
    "location": "Mumbai", 
    "job": "Engineer" 
} 
  1. Click on the Save button and the new document will be created.
  1. Once the document is created, we can query it using standard SQL-like queries. Click on the New SQL Query button and paste this query:
SELECT * FROM users 
  1. You can replace users with the collection name you provided. Do not add a semicolon as it shall throw an error.
  2. Run this query by clicking on the Execute Query button and you shall see the records as shown in the following screenshot. It may differ from your result:
  1. You can create new triggers and stored procedure from Data Explorer. For a demo purpose, let's create a simple stored procedure called helloworld, which upon execution returns the message Hello, World.
  2. In Data Explorer, click on stored procedure and create a new stored procedure. Give it a name, that is, helloworld and paste the following code and save it:
function storedProcedure(){ 
    var context = getContext(); 
    var response = context.getResponse(); 
    response.setBody("Hello, World"); 
} 
  1. On the left menu, click on Script Explorer and it will list the scripts we have created along with the stored procedure.
  2. Click on the helloworld procedure and click on the Execute button. This will run the stored procedure and show you the output as follows:

Similarly, you can create triggers as well and test it from Script Explorer.

  1. You can change the consistency levels from the default consistency settings. Choose any among the five consistency levels as shown in the following screenshot:

You will learn about the change feed feature when we try programming the SDKs of Cosmos DB in Chapter 5, Integrating Cosmos with Node.js.

Let's move ahead to the next database model that is, MongoDB.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.241