Jump to content

Exploring the Capabilities of Azure Cosmos DB for MongoDB using Open MongoDB shell.


Recommended Posts

Guest kevin_comba
Posted

largevv2px999.png.f86adc12c48ac30c15ba65935c0e676f.png

 

 

 

 

 

Previously we introduced Getting started with Azure Cosmos Database (A Deep Dive) blog which is an end-to-end introduction of Azure Cosmos DB. In this blog we are going to talk about one of the Azure Cosmos Database API known as Azure Cosmos Database for MongoDB.

 

You have been developing applications while leveraging the knowledge on MongoDB, as a developer, you are looking for a service that scales your database, offers automatic sharding without requiring any configuration, high availability and cost effective.

 

Azure Cosmos Db for MongoDB has you covered. In this blog, I will help you transition to start using the service. If you are a beginner and anxious about Azure Cosmos DB for MongoDB, be assured that you will learn from step-by-step guidance I will be sharing.

 

 

 

[HEADING=1]Topics Covered [/HEADING]

 

 

 

  • What is Mongo DB & Azure Cosmos DB for MongoDB
  • Provisioning Azure Cosmos DB for MongoDB vCore
  • Performing CRUD Operation in Open MongoDB(vCore) shell.

 

 

 

[HEADING=1]What is Mongo DB? [/HEADING]

 

 

 

Mongo DB is a popular NoSQL database, which is a document database known for its scalability, flexibility, and developer friendliness. Developers can interact with MongoDB using various drivers and APIs (Application Programming Interface).

 

What is Azure Cosmos DB for MongoDB?

 

It is a fully managed NoSQL, relational and vector database designed for modern app development. It is known for high response time.

 

 

 

[HEADING=1]Benefits of Azure Cosmos DB for MongoDB? [/HEADING]

 

 

 

  • Fast response time – ensure fats data access
  • Instantaneous scalability – Easy to scale up and down with zero warmup period.
  • Automatic sharding – Helps you focus with the application development as sharding is done automatically.
  • High availability – Ensures that that data is always available for you.
  • Cost effective – The database scales depending on your need. Beneficial as you pay for the resources you use.
  • Real-time analytics – runs analytics workloads against your data without affecting your database.

 

 

 

[HEADING=1]Create Azure Cosmos DB for MongoDB vCore Cluster on Azure. [/HEADING]

 

 

 

[HEADING=2]Prerequisites [/HEADING]

 

 

 

Azure account with active subscription. Create an account

 

 

 

[HEADING=3]Step 1: Create a Cluster [/HEADING]

 

 

 

  • Signing to Azure portal
  • Click Create a resource and search for Azure cosmos db.
  • Click on Azure cosmos DB for mongo DB

 

 

 

largevv2px999.jpg.3888e363943f06be3a146b95cff722cb.jpg

 

 

 

 

 

[HEADING=3]Step 2 Select vCore Cluster [/HEADING]

 

 

 

Azure Cosmos DB offers two types of resource architectures for MongoDB: Request Unit (RU)-based and vCore-based.

 

  • Request Unit (RU)-based - Request Units (RUs) serve as a performance currency in Azure Cosmos DB. Whether you perform writes, point reads, or queries, the costs are always measured in RUs.

  • vCore-based - This architecture allows you to use Azure Cosmos DB as if it were a MongoDB. It leverages vCores (virtual cores) to allocate resources based on your workload requirements.

 

Learn more about which model to choose between RU-based and vCore based

 

 

 

largevv2px999.jpg.3287c914c91f76d2704450f04328e0ca.jpg

 

 

 

 

 

[HEADING=3]Step 3: Fill the details to provision the resource. [/HEADING]

 

 

 

  • Click on configure to choose the resources you need for your resource.

 

largevv2px999.jpg.c1259679cac1386c5a85158090a531d1.jpg

 

 

 

 

 

  • I will go for the free tier because the resource is for demo purposes and not for production. Choose the resources that fit your workload.
  • If you never selected the free tier, remember to check the checkbox to agree to terms.
  • Click save after you are done with the configuration.

 

 

 

largevv2px999.jpg.8efe2e5f01523f15084e83e1cce49211.jpg

 

 

 

 

 

  • You need to fill in the details which you need for your cluster, let me break it down for you.
    • Subscription – select a subscription you want to use to create a cluster
    • Resource Group – select a resource group or you can create one.
    • Cluster name – The name should be globally unique.
    • Location – Select location near you.
    • MongoDB version – You can leave it to the default.
    • Admin username – provide administrator's name
    • Password – Provide a password which shall be used to access the database.

 

 

 

[HEADING=3]Step 3: Click on Networking [/HEADING]

 

 

 

  • Select Allow Public access from Azure Services. You can also add a client device to access the cluster.
  • Click Review and create
  • The deployment may take up to 5 mins.

 

 

 

largevv2px999.jpg.29faa50542f797f5bcaad5fc077746b9.jpg

 

 

 

 

 

  • After deployment is done, click on Go to Resource, you will be directed to the overview of your cluster you created.

 

 

 

[HEADING=3]Step 4: Connect to Open MongoDB Shell. [/HEADING]

 

 

 

  • We will use Open MongoDB shell provided with in Azure portal.
  • We are going to perform CRUD operation with the shell to simulate common operations.
  • As shown in the image below, click on Quick start, then Open MongoDB(vCore) shell.
  • You will be required to enter the password you provided earlier. Whenever you see [mongos] test>, it shows that you have connected successfully.

 

 

 

largevv2px999.jpg.cae9e645dddf34dadf11a55d0d1c07b7.jpg

 

 

 

 

 

[HEADING=1]Performing CRUD Operation in Open MongoDB(vCore) shell. [/HEADING]

 

Let us create a school management database on the MongoDB shell. We shall perform the Create, Read, Update and Delete operations.

 

[HEADING=3] [/HEADING]

[HEADING=3]Step1: Create the Database and Collections [/HEADING]

 

 

 

  • Connect to MongoDB shell

 

 

 

[iCODE]use KirinyagaSchool [/iCODE]

 

 

 

  • Create students' collection

 

 

 

[iCODE]db.createCollection("students") [/iCODE]

 

 

 

  • Create staff collection

 

 

 

[iCODE]db.createCollection("staff") [/iCODE]

 

 

 

 

 

 

 

largevv2px999.png.47ca8826fc559af964f80c296da41620.png

 

 

 

[HEADING=3] Step 2: Define and insert Sample data. [/HEADING]

 

 

 

  • Insert sample students

 

 

 

db.students.insertMany([ 

   { 

       student_id: 1, 

       first_name: "Mike", 

       last_name: "Kamau", 

       gender: "Male", 

       age: 14, 

       address: "1234 Nairobi Lane, Nairobi", 

       class: "8A" 

   }, 

   { 

       student_id: 2, 

       first_name: "Brian", 

       last_name: "Kemboi", 

       gender: "Male", 

       age: 13, 

       address: "5678 Eldoret Road, Eldoret", 

       class: "7B" 

   }, 

   { 

       student_id: 3, 

       first_name: "Jane", 

       last_name: "Wanjiku", 

       gender: "Female", 

       age: 15, 

       address: "91011 Kisumu Street, Kisumu", 

       class: "8A" 

   } 

]) 

 

 

 

  • Insert sample staff

 

 

 

db.staff.insertMany([ 

   { 

       staff_id: 1, 

       first_name: "Alice", 

       last_name: "Muthoni", 

       gender: "Female", 

       position: "Teacher", 

       salary: 50000, 

       address: "1213 Nyeri Avenue, Nyeri" 

   }, 

   { 

       staff_id: 2, 

       first_name: "John", 

       last_name: "Otieno", 

       gender: "Male", 

       position: "Principal", 

       salary: 80000, 

       address: "1415 Mombasa Road, Mombasa" 

   }, 

   { 

       staff_id: 3, 

       first_name: "Esther", 

       last_name: "Njeri", 

       gender: "Female", 

       position: "Secretary", 

       salary: 40000, 

       address: "1617 Thika Highway, Thika" 

   } 

]) 

 

 

 

 

 

 

 

[HEADING=3]Step 3: Perform CRUD Operations [/HEADING]

 

 

 

[HEADING=2]Create [/HEADING]

 

 

 

  • To insert a new student:

 

 

 

db.students.insertOne({ 

   student_id: 4, 

   first_name: "Peter", 

   last_name: "Kariuki", 

   gender: "Male", 

   age: 12, 

   address: "1819 Nakuru Lane, Nakuru", 

   class: "6C" 

}) 

 

 

 

  • To insert a new staff member:

 

 

 

db.staff.insertOne({ 

   staff_id: 4, 

   first_name: "Daniel", 

   last_name: "Mwangi", 

   gender: "Male", 

   position: "Caretaker", 

   salary: 30000, 

   address: "2021 Meru Street, Meru" 

}) 

 

 

 

 

 

[HEADING=2]Read[/HEADING]

 

 

 

  • To find a student by last name:

 

 

 

[iCODE]db.students.find({ last_name: "Kemboi" }) [/iCODE]

 

 

 

 

 

 

 

largevv2px999.png.c72613d1a970d33e95af5a15bbcf024e.png

 

  • To get all staff members:

 

 

 

[iCODE]db.staff.find() [/iCODE]

 

 

 

 

 

[HEADING=2] Update[/HEADING]

 

 

 

  • [HEADING=3]To update a student's class: [/HEADING]

 

 

 

db.students.updateOne( 

   { student_id: 1 }, 

   { $set: { class: "9A" } } 

) 

 

 

 

 

 

 

 

largevv2px999.png.a77b4c32c087f25ecc0bce895d0b7167.png

 

  • To increase the salary of a staff member:

 

 

 

db.staff.updateOne( 

   { staff_id: 2 }, 

   { $inc: { salary: 5000 } } 

) 

 

 

 

 

 

[HEADING=2]Delete[/HEADING]

 

 

 

  • To delete a student:

 

 

 

[iCODE]db.students.deleteOne({ student_id: 4 }) [/iCODE]

 

 

 

  • To delete a staff member:

 

 

 

[iCODE]db.staff.deleteOne({ staff_id: 4 }) [/iCODE]

 

 

 

 

 

The expected output

 

 

 

largevv2px999.png.d99f2183004a7bf3015071b5c3bf63a9.png

 

 

 

 

 

We achieved this blog's objectives, working with Azure cosmos DB for MongoDB is like MongoDB on Atlas. We have also known the advantages of Azure cosmos DB for MongoDB. Try practicing more by creating applications while using the content learnt.

 

 

 

[HEADING=1]Read More. [/HEADING]

 

 

 

 

 

 

[HEADING=1]SDKs [/HEADING]

 

 

 

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...