The MongoDB data model

MongoDB is one of the most popular open source NoSQL databases in the world. MongoDB is one of the key competitors to Microsoft's DocumentDB. MongoDB provides community license and enterprise version for organizations.

You can use MongoDB as a data model in Cosmos DB. If your existing application already uses MongoDB, then all you need to change is the connection parameter and your application will work! It is as smooth as that.

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

  • Elastic scale: Effectively scale up or downsize your database to meet your application needs. Your information is stored on an SSD for low latency. Cosmos DB supports the MongoDB format, that is, collections that can be scaled and used for a large set of records.
  • Replication: As you have learned in the last chapter, Cosmos DB data models, such as MongoDB can be replicated across any number of regions supported by the Azure data center. This provides us with the ability to develop a globally available application 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.
  • Compatibility: Cosmos DB keeps the MongoDB API and SDKs as independent as possible. You can use your existing MongoDB expertise, code, and APIs to work with MongoDB and scale it. Cosmos DB provides fully managed MongoDB along with the  other features to help you scale and manage it properly.
  • Fully managed: You don't need to install or run the MongoDB daemon or server anymore. It is fully managed by Microsoft. Fear of restarts, crashes, and being out of memory are no more!
  • Automatic indexing: Cosmos DB indexes all documents of MongoDB for faster response time and low latency.

Let's go ahead and create a MongoDB data model in Cosmos DB:

  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; choose the MongoDB option from the API drop down.
  3. You can create a new resource or use the 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 MongoDB API and notify you of the same.

  1. Once the MongoDB data model is created, we can create new collections in it and store the data.
  2. Go to Data Explorer on the left menu and click on the New Collection button. 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.

Once the document is created, we can query it using standard MongoDB queries.

  1. Click on the New Mongo Query button and paste the preceding mentioned query.
  2. Run this query by clicking on the Execute Query button and you shall see the following records as shown in the next screenshot. It may differ from your result:

Similarly, we can delete these documents or update them using Data Explorer. You can also create stored procedures and triggers as we have discussed in the previous section DocumentDB API.

MongoDB is one of the key database models in Cosmos DB. One of the core reasons is due to its popularity. The majority of developers and enterprises are already using it, and it would be a great customer base if they migrate to Cosmos. This is the key reason why they describe it as an easy-to-migrate model; otherwise, why would Microsoft offer a competitor as an option!

You will learn about the SDKs and how to integrate them with our custom application in Chapter 6, Building High-Performance Link Shortening and Tracking System using Cosmos DB.

As for the consistency and replication, you can use any consistency levels among the five provided by Cosmos with MongoDB.

You can also distribute it across any number of regions provided by Azure as discussed in Chapter 2, Cosmos DB Global Turnkey Distribution.

Let's go ahead and check out the third data model that is Tables!

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

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