K
kevin_comba
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.
Topics Covered
- What is Mongo DB & Azure Cosmos DB for MongoDB
- Provisioning Azure Cosmos DB for MongoDB vCore
- Performing CRUD Operation in Open MongoDB(vCore) shell.
What is Mongo DB?
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.
Benefits of Azure Cosmos DB for MongoDB?
- 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.
Create Azure Cosmos DB for MongoDB vCore Cluster on Azure.
Prerequisites
Azure account with active subscription. Create an account
Step 1: Create a Cluster
- Signing to Azure portal
- Click Create a resource and search for Azure cosmos db.
- Click on Azure cosmos DB for mongo DB
Step 2 Select vCore Cluster
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
Step 3: Fill the details to provision the resource.
- Click on configure to choose the resources you need for your resource.
- 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.
- 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.
Step 3: Click on Networking
- 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.
- After deployment is done, click on Go to Resource, you will be directed to the overview of your cluster you created.
Step 4: Connect to Open MongoDB Shell.
- 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.
Performing CRUD Operation in Open MongoDB(vCore) shell.
Let us create a school management database on the MongoDB shell. We shall perform the Create, Read, Update and Delete operations.
Step1: Create the Database and Collections
- Connect to MongoDB shell
use KirinyagaSchool
- Create students' collection
db.createCollection("students")
- Create staff collection
db.createCollection("staff")
Step 2: Define and insert Sample data.
- Insert sample students
Code:
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
Code:
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"
}
])
Step 3: Perform CRUD Operations
Create
- To insert a new student:
Code:
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:
Code:
db.staff.insertOne({
staff_id: 4,
first_name: "Daniel",
last_name: "Mwangi",
gender: "Male",
position: "Caretaker",
salary: 30000,
address: "2021 Meru Street, Meru"
})
Read
- To find a student by last name:
db.students.find({ last_name: "Kemboi" })
- To get all staff members:
db.staff.find()
Update
Code:
db.students.updateOne(
{ student_id: 1 },
{ $set: { class: "9A" } }
)
- To increase the salary of a staff member:
Code:
db.staff.updateOne(
{ staff_id: 2 },
{ $inc: { salary: 5000 } }
)
Delete
- To delete a student:
db.students.deleteOne({ student_id: 4 })
- To delete a staff member:
db.staff.deleteOne({ staff_id: 4 })
The expected output
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.
Read More.
- Comparing MongoDB Atlas and Azure Cosmos DB for MongoDB
- Azure Cosmos DB for MongoDB on Microsoft Learn
- Use Azure Data Studio to connect and query Azure Cosmos DB API for MongoDB
- Migrate to vCore-based Azure Cosmos DB for MongoDB
SDKs
- Azure Cosmos DB for MongoDB for .NET with the MongoDB driver
- Azure Cosmos DB for MongoDB driver for Node.js
- Azure Cosmos DB for MongoDB for Python with MongoDB driver
- Create a console app with Java and Azure Cosmos DB for MongoDB
- Connect a Go application to Azure Cosmos DB for MongoDB
Continue reading...