DEV Community

Ariful Haque Sajib
Ariful Haque Sajib

Posted on

Running MongoDB in Docker: A Simple Guide

Image description


Running MongoDB in Docker: A Simple Guide By Md Ariful Haque Sajib (arifulhaque313)

Quick Steps

Setting Up MongoDB

  1. Pull the MongoDB image
   docker pull mongo
Enter fullscreen mode Exit fullscreen mode
  1. Run MongoDB container
   docker run --name mongodb -d -p 27017:27017 mongo
Enter fullscreen mode Exit fullscreen mode

This runs MongoDB on port 27017

  1. Check running containers
   docker ps
Enter fullscreen mode Exit fullscreen mode
  1. Connect to MongoDB
   docker exec -it mongodb mongo
Enter fullscreen mode Exit fullscreen mode
  1. Connect to MongoDB shell
   docker exec -it mongodb mongosh
Enter fullscreen mode Exit fullscreen mode

Essential MongoDB Commands

Basic Database Operations

  1. Show all databases
   show dbs
Enter fullscreen mode Exit fullscreen mode
  1. Use a database
   use db_name
Enter fullscreen mode Exit fullscreen mode
  1. Show collections
   show collections
Enter fullscreen mode Exit fullscreen mode
  1. Create a database
   db.createDatabase("database_name")
Enter fullscreen mode Exit fullscreen mode
  1. Delete a database
   db.dropDatabase("database_name")
Enter fullscreen mode Exit fullscreen mode

Working with Collections and Documents

  1. Create collection
   db.createCollection("collectionName")
Enter fullscreen mode Exit fullscreen mode
  1. Insert documents
   db.collection_name.insertOne({name: "Md Ariful Haque Sajib"})
   db.collection_name.insertMany([{name: "John"}, {name: "Sarah"}])
Enter fullscreen mode Exit fullscreen mode
  1. Update document
   db.users.updateOne({name: "Ariful Haque"}, {$set: {age: 27}})
Enter fullscreen mode Exit fullscreen mode
  1. Delete document
   db.users.deleteOne({name: "Ariful Haque"})
Enter fullscreen mode Exit fullscreen mode

Data Persistence

For keeping your data when container restarts:

docker run --name mongodb -d -p 27017:27017 -v /your/local/path:/data/db mongo
Enter fullscreen mode Exit fullscreen mode

Enabling Security

Create MongoDB with authentication:

docker run --name mongodb -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password mongo
Enter fullscreen mode Exit fullscreen mode


I have a project simple project on express js and mongoDB. You can Check out,
πŸ“‚ Git Repository: Crud-API-Mongo-Express


I'm Md Ariful Haque Sajib, a Software Engineer πŸ‘¨β€πŸ’». Specializing in PHP, Laravel, Vue.js, and React.js. With expertise in E-commerce, Payment Gateways, APIs, various Management and Finance-based software. I am passionate about 🧠 learning and delivering 🌟 high-quality solutions.


πŸ“Œ Follow me for updates and insights:
🌐 GitHub: ArifulHaque313
πŸ”— LinkedIn: Md Ariful Haque Sajib
πŸ“§ Contact: asajib7654@gmail.com


πŸ“’ Hashtags:
 #Ariful_Haque_Sajib #MongoDB #Docker #Database #WebDev #arifulhaque313
Hope this makes it more visually appealing! 😊

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video β†’

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DreamFactory generates live REST APIs from database schemas with standardized endpoints for tables, views, and procedures in OpenAPI format. We support on-prem deployment with firewall security and include RBAC for secure, granular security controls.

See more!

πŸ‘‹ Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spiritsβ€”leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay