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

ItemTypical Value
Hostlocalhost
Port27017
Connection URImongodb://localhost:27017/mydb

Installation and Startup

macOS:

brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community

Ubuntu (see MongoDB Official Documentation for installation after adding the repository).

Docker:

docker run -d --name mongo -p 27017:27017 mongo:7

Command Line

mongosh
use mydb
db.users.insertOne({ name: "Alice" })
db.users.find()

Framework Integration

FrameworkDescription
NestJS@nestjs/mongoose, URI points to localhost:27017
Expressmongoose.connect('mongodb://localhost:27017/myapp')
Next.jsEnvironment 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.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run