Everything about MongoDB

Everything about MongoDB

Mongoose, mongoose model, MVC architecture, request-response cycle, Application logic, Business Logic and many more

What is MongoDB?

MongoDB is nothing but just a database and it is a no-SQL database which is written in c++ language. It is a document-based database with the scalability and flexibility that you want with the querying and indexing that you need. It uses JSON and the binary version of JSON. It is not like a traditional database which stores the data in a 2-dimensional row and column structure.

How to use MongoDB?

The answer is very simple just go through the official documentation of MongoDB and download the latest version as per your OS(Operating system). once you finish downloading then you have to open an instance of it in your terminal with the help of mongod command. After this just open a new terminal and write mongo in it then you can start using MongoDB by creating a new database, queries and many more.

oops! I don't know why I am writing this installation guide here which is already available in the official documentation.

let's continue the blog ahead.

What is Mongoose?

so mongoose is an animal, right? but in the database field mongoose is not an animal it is an Object Data Modeling (ODM) library. We all know what a library is and the mongoose is just another library. It provides a high level of abstraction.

So now what the hell is that ODM?

ODM is a library or just a way for us to write javascript code that will interact with database.

Mongoose allows us for rapid and simple development of MongoDB database interactions, features, schemas to model data and relationships, easy data validation, simple query API, middleware and blah blah blah...

These are all things you will only understand when you start using MongoDB.

What is Mongoose schema?

In that, we model our data by describing the structure of the data, default values and validation.

Let's say we want to handle or add some data about our exams

The first and important thing is total marks in an exam, right?

so we can define the type of total marks in our mongoose schema and obviously, the type will be a Number. So if a user tries to enter a String in the total marks section then the mongo will throw a validation error. Because we have defined that the type should be a Number.

not only just TYPE we can add lots of validation to a mongoose schema like required, minlength and many more you can find out these methods or schemas in MongoDB documentation.

Mongoose Model

this is a wrapper for the schema providing an interface to the database for CRUD operations.

SCHEMA --> MODEL

CRUD operations

CRUD stands for Create Read Update Delete, as the name suggests these operations create the data using Create operation and reads the data using the Read operation and update the data using the Update operation and the same for delete as well. So the big question is how to perform these operations. We can perform these operations using the Postman tool. You can download it from the official documentation.

Let's move on

MVC Architecture(Model View Controller)

let's see first what is Modle?

In this architecture, the model layer is only concerned with the application data and the business logic of an app. It simply means that the Model layer focuses on the data of the users and how the company will use this data to expand their business.

Next is the view layer

The view layer is necessary if we have a graphical interface in our app, View layer consists of the template used to generate a view. It sends the HTML files in response if it is present.

Controller layer

Next up we have a controller layer and the function of the controller is to handle the application request interact with models and send back a response to the client all that is called the application logic.

Request-Response cycle

So it all starts with a request that request will hit one of our routers. Now the goal of the router is to delegate the request to the correct handler function which will be in one of the controllers. depending on the user's request controller might need to interact with one of the models to retrieve a certain document from a database or to create a new one. After getting data from the model the controller might then be ready to send back a response to the client for containing the data. There is one more step is involved. If the user wants a graphical interface then the controller will select one view template to render.

Difference between Application Logic and Business Logic

Business Logic :

So you might be wondering is there a business logic also? The answer is yes every website or app has its specific business logic.

Let's say there is a website of a restaurant and the restaurant only serves food when you pay the bill online.

The above example might be confusing though.

Let's say there is a website of a video platform and the company only wants to show the videos to subscribed users. here the login id and password of the subscribed users will be different obviously. here we have to build the logic as per the business requirement.

Business logic is the code that solves the business problem and It is directly related to business rules like how the business works and business needs.

Application Logic :

And the application logic is only concerned with the app implementation, not the underlying business problem we are trying to solve.

It is concerned with managing requests and responses.

It is the more technical aspect.