

The local MongoDB server and issue commands from your JavaScript program as follows: // import MongoDB driver const MongoClient = require( 'mongodb').

Once the official Mongo driver is installed, you can connect to The official Mongo driver: $ npm install mongodb To connect to MongoDB from Express and Node, you will first need to install If you’ve been following along so far, your container is currently running in detached mode.MongoDB, Node, Connection Pooling, and MVC MongoDB, Node, Connection Pooling, and MVC Installing Mongo Driver

Rather than creating an application to make use of it, we’re going to interact with the database through the shell client. Interacting with the MongoDB Docker Container with Basic Shell OperationsĪt this point in time we have a functional MongoDB deployment. If you plan to deploy all your applications as micro-services with Docker, then you’d be fine as long as your containers can communicate with each other. At this point, you can run your backend and have a successful connection with the mongodb (MongoDB must be up and running). Like I said, the port mapping is only necessary if you wish to use it from your host Mac, Windows, or Linux computer. After this, you can add a run script inside your package.json file, under scripts: 'server': 'nodemon index.js'. You don’t need to map the ports in order to use MongoDB. The ports used were taken from the MongoDB documentation.

We are also mapping the container ports with host ports so that way we can access the database from a host level application if we wanted to. The above command will run the container in detached mode, or in the background for us. The simplest solution to download our image is to execute the following:ĭocker run -d -p 27017-27019:27017-27019 -name mongodb mongo:4.0.4 There are numerous images available, each representing different versions of MongoDB or the underlying operating system that it is installed on. Downloading the Latest MongoDB Docker ImageĪssuming that you have Docker installed on your computer or server, we can obtain the MongoDB image from the Docker Hub container registry. In this tutorial we’re going to focus on using Docker to deploy MongoDB as a container and interact with it with the shell client. However, you can also install MongoDB on premise using multiple options. For example, I had recently written a tutorial titled, Developing a RESTful API with Node.js and MongoDB Atlas which focused on the MongoDB cloud solution called Atlas. There are plenty of deployment options when it comes to using MongoDB. So what if you wanted to give MongoDB a spin and see what it’s all about? MongoDB is one of the most popular NoSQL databases on the market right now and is used heavily with Node.js development in particular.
