Back to Roadmap
5:00

MongoDB Basics

Learn the fundamentals of MongoDB and understand how NoSQL databases are used in modern Node.js applications

5 MIN READ VERIFIED CURRICULUM

MongoDB is a popular NoSQL database used for storing and managing application data efficiently.

It stores data in flexible JSON-like documents instead of traditional tables and rows.

What is MongoDB?

MongoDB is a document-oriented NoSQL database designed for scalability, flexibility, and high performance.

It is widely used with Node.js applications because both technologies work naturally with JSON data.

SQL vs NoSQL

Traditional SQL databases use tables, rows, and fixed schemas.

NoSQL databases like MongoDB use collections and documents with flexible structures.

SQL: Tables -> Rows
MongoDB: Collections -> Documents
text

Understanding Documents

Documents are the basic units of data in MongoDB.

They are stored in BSON format, which is similar to JSON.

{
  "name": "John",
  "email": "john@example.com",
  "age": 24
}
json

Understanding Collections

Collections are groups of related documents in MongoDB.

A collection is similar to a table in relational databases.

Installing MongoDB

MongoDB can be installed locally or accessed using cloud services like MongoDB Atlas.

MongoDB Shell

The MongoDB shell allows developers to interact with databases using commands.

mongosh
bash

Creating a Database

MongoDB automatically creates a database when data is inserted.

use myDatabase
javascript

Creating a Collection

db.createCollection('users')
javascript

Inserting Documents

Documents can be inserted into collections using insertOne or insertMany methods.

db.users.insertOne({
  name: 'John',
  age: 24
})
javascript

Retrieving Documents

The find method is used to retrieve documents from collections.

db.users.find()
javascript

Filtering Documents

MongoDB allows filtering documents using query conditions.

db.users.find({ age: 24 })
javascript

Updating Documents

Documents can be updated using updateOne or updateMany methods.

db.users.updateOne(
  { name: 'John' },
  { $set: { age: 25 } }
)
javascript

Deleting Documents

MongoDB provides methods for deleting documents from collections.

db.users.deleteOne({ name: 'John' })
javascript

MongoDB with Node.js

MongoDB can be connected with Node.js applications using the MongoDB driver or Mongoose ODM.

npm install mongoose
bash

Connecting MongoDB with Mongoose

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/myDatabase')
  .then(() => console.log('Database Connected'));
javascript

Advantages of MongoDB

MongoDB provides flexible schemas, scalability, and fast performance for modern applications.

It is highly suitable for applications with rapidly changing data structures.

Common Beginner Mistakes

Beginners often confuse collections with documents and forget proper query filtering.

Another common mistake is not handling database connection errors properly.

Best Practices

Use schema validation and indexes to improve database consistency and performance.

Store only required data and design collections carefully for scalability.

Real-World Importance

MongoDB is widely used in modern web applications, cloud systems, and scalable backend services.

Many startups and enterprise companies use MongoDB with Node.js for high-performance applications.

Summary

MongoDB is a flexible NoSQL database that stores data using collections and documents.

Learning MongoDB fundamentals is essential for backend developers building modern Node.js applications.