Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Using Mongoose Framework to Access Azure Cosmos DB MongoDB API

Cosmos DB is a globally-distributed, multi-model database service on Microsoft Azure. Cosmos DB provides database services like SQL API, MongoDB API, Cassandra, Gremlin for storing NoSQL data.

We can create a Cosmos DB account to create a database and perform Read/Write operations on these databases using various SDKs like .NET, .NET Core, Node.js, JAVA etc. If you are working on  theJavaScript stack for building isomorphic apps using Node.js and other JavaScript frameworks like Angular, Vue.js or libraries like React.js, then you will mostly prefer to use MongoDB database for storing data. 

Most of the isomorphic apps those which are using MongoDB database, uses Mongoose framework to perform CRUD operations on the MongoDB Database. On Microsoft Azure, if you are using MongoDB API database service of Cosmos DB to store data,   there is a support available for Mongoose framework. This provides a similar coding experience of using MongoDB in on-premise applications. 

In this article, we will see how to access Azure Cosmos DB for MongoDB API using Node.js, Express application. Figure 1 explains the application structure

Creating Web Applications using MERN Stack

MongoDBMongoDB is a cross-platform document-oriented database program. It is a NoSQL Database. The data is stored in JSON document format and this database can store images in Base64 encoded format. More information on MongoDB can be read from this link.


Express.js: This is a Web Application Framework for Node.js. We can use this for building REST APIs and also for building Web Applications. More information on Express.js can be read from this link.

React.js makes it painless to create modern front-end applications. This library provides an easy approach for building interactive views. 

Node.js: This is an open-source, cross-platform JavaScript runtime environment used to run JavaScript code out-of-browser. We use Node.js for server-side JavaScript execution. More information can be read from this link.

The Component is a major building block of a React.js application. Component is the object that contains data, logic and user interface (UI) for the View. We can build an application with complex Views  using React.js components. 

In JavaScript full stack applications, we can use JavaScript object model for End-to-End apps that varies  from UI to Database. We use MongoDB, Express.js, React.js and Node.js for designing such applications, which are collectively also knows as MERN apps. In this article, we will build a complete JavaScript application based on MERN a.k.a the MERN stack.   

Dockerizing a Node, Express and MongoDB Application

In this tutorial, we will dockerize a Node,Express and Mongo Application. We will be using a Docker Linux container.

Let’s first take an overview of Docker and then we will directly jump to the implementation of the application.

What is Docker and why to use it?


When our applications are ready for  deployment, we have to configure a production machine with all the dependencies for the application. This is often a tedious and time consuming job. To make this approach easy, we can make use of Docker.

Docker provides a way to run applications securely and isolated in a container with all the applications dependencies packaged in it. Docker containers wrap up software and its dependencies into a standardized unit for software development. This makes sure that all the dependencies to run the application are packaged so that application will always run.

Some additional advantages of using Docker are:
  1. Once a docker image is created with all its dependencies, you need not worry about the operating system configuration for your application deployment. Since Docker is platform independent, a docker image that has successfully build  can run on any platform using docker.
  2. This docker image can be easily shared with a client (or anyone) who wants to use the application. They can run the application simply by installing docker on their machine.        
Docker can be downloaded from this link.