MongoDB Local Usage Guide
Install and connect to MongoDB on localhost:27017 for local development with MERN stack, NestJS Mongoose, and more.
MongoDB is a popular document-oriented NoSQL database, commonly used by the MERN/MEAN stack and many Node.js projects. The local instance listens on localhost:27017 by default.
Default Connection
| Item | Typical Value |
|---|---|
| Host | localhost |
| Port | 27017 |
| Connection URI | mongodb://localhost:27017/mydb |
Installation and Startup
macOS:
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-communityUbuntu (see MongoDB Official Documentation for installation after adding the repository).
Docker:
docker run -d --name mongo -p 27017:27017 mongo:7Command Line
mongosh
use mydb
db.users.insertOne({ name: "Alice" })
db.users.find()Framework Integration
| Framework | Description |
|---|---|
| NestJS | @nestjs/mongoose, URI points to localhost:27017 |
| Express | mongoose.connect('mongodb://localhost:27017/myapp') |
| Next.js | Environment variable MONGODB_URI |
GUI Tools
- MongoDB Compass (official GUI)
- TablePlus, Studio 3T
Frequently Asked Questions
Port 27017 Conflict
There may be an existing MongoDB instance or a Docker mapping conflict; use lsof -i :27017 to troubleshoot.
Authentication
Local development often does not require a username and password; ensure to enable auth in production.
Summary
MongoDB defaults to localhost:27017 for local use, and Docker installation is the quickest method, making it a common choice for full-stack Node.js applications and document storage.