Net Core 6 with MongoDb and Azure Cosmos For Mongo- MacOS

Barkın Kızılkaya
Geek Culture
Published in
4 min readJun 20, 2022
https://www.pexels.com/

Let’s start with the definition of MongoDB

MongoDB is a NoSQL database we can also say a non-relational database

Relational Database

Has tables, columns, rows, and even the relation between tables and entities. And uses structured query language or SQL

Non-Relational Database

It doesn’t have a tabular schema of rows and columns. Based on data structure like documents and collection.

For the topic of this article, we will not deep into Mongo DB. But we will create a restful API then we will write some CRUD operations. And finally, we will use Azure Mongo DB in our application.

And because there is not enough source we will start from the installation of MongoDB to MacOs

For the installation, we will use Homebrew. So we need to install homebrew first.

We need to run this command on macos terminal

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installation is finished. We can start to install MongoDB. So we can follow these steps from the official page. After installation finishes, we need to run MongoDB on our local computer.

To create MongoDB we need an additional tool. Which is MongoDB Compass. It can be downloaded from its page.

If we can open the MongoDB compass there will be connected button. After clicking this button it will connect our local MongoDB.

We should click the CreateDatabase button. Then it will go to ask for Database Name and Collection Name. For this example, I will create UserDb and Users Collection.

Then we need to select the database which we create. We will see our collection. With the add data button. We can open the insert document screen.

But because we will use rest API to feed our DB we can delete data.

To Use MongoDb in our project we need to install MongoDB.Driver package from NuGet.

Then we need to open the AppSettings file and we should create ConnectionString. You can learn your port number from the compass app. Your post number has to be the same as your port number.

We will create a service to handle CRUD operations.

But before that, we can look at how we can query MongoDB

GetAllUsers : Find(u=>true).ToListAsync();
GetUserById : Find(u=>u.id == id).FirstOrDefaultAsync();
AddUser : InsertOneAsync(user);
UpdateUser : ReplaceOneAsync( u=>u.id == id,user);
DeleteUser : DeleteOneAsync(u =>u.id == id);

With this information, we can create User Service

Now we can use this service from the controller.

Now we can try to run the project.

Easy isn’t it ?

Now let’s use mongo in Azure Cosmos. Why Cosmos? Because cosmos is a no SQL and it can work with MongoDB.

From the Azure portal search azure cosmos then select Azure Cosmos DB API for MongoDB

Then click Create then select the serverless option because it is cheaper

It will take some time to create. After the creation process is finished go to resource then select data explorer. from this page select new collection. Set DB name and table name then create an ok button.

from the side, menu select the connection string which is located in Settings Section.

Copy PRIMARY CONNECTION STRING from settings. And change the connection string which we wrote before to the appSettings file.

if you run the project again. You will see data in azure cosmos DB data explorer!

And this is the end of this article!

Have a good day…

--

--