Namraj Pudasaini
Jul 18, 2026
A quick reference for getting MongoDB running on Windows and connecting to it from your applications.
To start the MongoDB server on Windows, open Command Prompt and run:
mongod.exe --dbpath "C:\data\db"
Make sure the C:\data\db directory exists before running this command. MongoDB stores its data there.
If you are using Git Bash or WSL, start with cmd.exe first:
cmd.exe
mongod.exe --dbpath "C:\data\db"
If you installed MongoDB with the Windows installer and left the service option enabled, you do not need to start mongod by hand — it starts with Windows. To check, open services.msc and look for the MongoDB Server entry. Its status should read Running.
You can do the same from an Administrator Command Prompt:
net start MongoDB
If the service is already running you get a message saying so, which is harmless.
When nothing is listening on the port, every client fails the same way:
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
That error means the server is down, not that your connection string is wrong. Start the service or run mongod manually before debugging anything else.
Once the server is running, connect using the MongoDB shell:
mongosh mongodb://localhost:27017
This opens an interactive shell where you can run queries, create databases, and manage collections. mongosh is distributed separately from the server, so if Windows reports that the command is not recognised, install the MongoDB Shell package and confirm its folder is on your PATH.
A handful of commands cover most day-to-day use:
show dbs
use myapp
show collections
db.users.find()
db.users.find({ name: "Alice" })
use myapp switches to that database and creates it lazily — it will not show up in show dbs until you write the first document into it. db.users.find() returns the documents in the users collection.
Mongoose is the most popular MongoDB ODM for Node.js. Here is a basic connection:
const mongoose = require("mongoose");
mongoose
.connect("mongodb://127.0.0.1:27017/myapp")
.then(() => console.log("Connected to MongoDB!"))
.catch((err) => console.error("Could not connect to MongoDB:", err));
Use 127.0.0.1 instead of localhost — this avoids DNS resolution issues in Node.js 18+.
For production apps, store your database URL in a .env file:
DATABASE_URL="mongodb://localhost:27017"
Then load it in your code:
require("dotenv").config();
mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", (error) => console.error(error));
db.once("open", () => console.log("Connected to Database"));
Rule: never hardcode connection strings in source code. Use
.envfiles and add.envto.gitignore.
Your application code does not change when you move to MongoDB Atlas — only the connection string does. A local instance uses mongodb://127.0.0.1:27017/myapp. Atlas gives you an SRV string shaped like mongodb+srv://user:password@cluster.mongodb.net/myapp, and you have to add your current IP address to the cluster's network access list before it will accept the connection. Keeping the URL in .env is what makes swapping between the two a one-line change.
Next steps: once connected, explore Mongoose schemas to define your data models, and MongoDB Atlas for cloud-hosted databases.
Tools mentioned: DigitalOcean for cloud hosting and managed databases.