Skip to main content
D
Node.js
MongoDB with Node.js

MongoDB with Node.js

MongoDB pairs perfectly with Node.js to store database records.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

Node.js applications need a place to save data permanently. MongoDB is a NoSQL database that stores data exactly like JavaScript objects, making it the perfect match for Node.js developers.

Example

Example
1/* this connects to MongoDB */
2const mongoose = require('mongoose');
3
4mongoose.connect('mongodb://localhost/my_db');
5
6const UserSchema = new mongoose.Schema({
7  name: String,
8  age: Number
9});

Key Points

  • MongoDB is a NoSQL, document-based database.
  • It stores data in flexible JSON-like documents.
  • Mongoose is the most popular library to connect Node.js and MongoDB.
  • It is the "M" in the popular MERN stack.

Up Next

Continue your journey with the next topic.

Go to Environment Variables