- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js MongoDB Get Started
MongoDB is a leading document-oriented NoSQL database designed for high performance and high availability. Unlike traditional relational databases that use tables and rows, MongoDB stores data in BSON (Binary JSON) format, which aligns perfectly with JavaScript objects. This synergy makes MongoDB the go-to choice for Node.js developers building modern web applications, APIs, and real-time services.
Key Features of MongoDB with Node.js
- Document-Based Storage: Data is stored in flexible, JSON-like documents. This means you don't need to define a rigid schema before adding data, allowing your data model to evolve as your application grows.
- High Scalability: MongoDB is designed for horizontal scaling (sharding), making it capable of handling massive amounts of data and high traffic loads with ease.
- Native Node.js Driver: The official
mongodbpackage provides an asynchronous API that uses Promises andasync/await, making database interactions feel like native JavaScript. - Rich Query Language: Beyond simple CRUD, MongoDB supports complex aggregations, geospatial queries, and full-text search.
Step 1 Install MongoDB and the Node.js Driver
Before writing any code, you need a running MongoDB instance and the driver installed in your project folder.
- Install MongoDB
- Local: Download the Community Server from the official website.
- Cloud: Alternatively, use MongoDB Atlas, a free-tier managed cloud service that removes the need for local installation.
- Initialize your project
If you haven't already, create a project folder and initialize it:
mkdir my-mongo-app && cd my-mongo-app npm init -y - Install the MongoDB Node.js Driver
Run the following command to add the driver to your dependencies:
npm install mongodb
--save flag (or let npm do it by default) to ensure the dependency is recorded in your package.json file, making it easier for team members to install later.
Step 2 Connect to MongoDB
To interact with the database, we use the MongoClient class. We'll establish a connection using a connection string, which typically points to localhost (127.0.0.1) for local development.
Example Code
const { MongoClient } = require('mongodb');
// Connection URL (127.0.0.1 is preferred over 'localhost' in some Node versions)
const url = 'mongodb://127.0.0.1:27017';
const dbName = 'inventory_system';
async function main() {
const client = new MongoClient(url);
try {
// Attempt to connect to the server
await client.connect();
console.log('Successfully connected to MongoDB server');
// Access or create the database
const db = client.db(dbName);
console.log(`Using Database: ${db.databaseName}`);
// You are now ready to perform operations!
} catch (error) {
console.error('Connection failed:', error);
} finally {
// Ensuring the client will close when you finish/error
await client.close();
}
}
main().catch(console.error);
brew services or systemctl.
Output:
Connected to MongoDB
Database: inventory_system
Step 3 Perform CRUD Operations
CRUD stands for Create, Read, Update, and Delete. These are the four basic functions of persistent storage.
Create (Insert Data)
In MongoDB, a single record is called a Document, and a group of documents is called a Collection.
async function insertData(db) {
const collection = db.collection('products');
const result = await collection.insertOne({
item: 'Mechanical Keyboard',
stock: 45,
tags: ['electronics', 'peripheral'],
lastUpdated: new Date()
});
console.log('Document inserted with _id:', result.insertedId);
}
_id for every document. You don't need to provide one unless you have a specific custom ID requirement.
Output:
Inserted document: 61b0cfe1f6b4f1e749bc6c4e
Read (Query Data)
Fetching data can be done for a single document (findOne) or multiple documents (find). When using find, MongoDB returns a cursor, which you can convert to an array.
async function fetchData(db) {
const collection = db.collection('products');
// Find a specific product by name
const product = await collection.findOne({ item: 'Mechanical Keyboard' });
console.log('Found product:', product);
}
{} as the query filter: collection.find({}).toArray().
Output:
Fetched document: { "_id": "61b0cfe1f6b4f1e749bc6c4e", "item": "Mechanical Keyboard", "stock": 45 ... }
Update (Modify Data)
To modify a document, we use atomic operators like $set. This prevents you from accidentally overwriting the entire document when you only want to change one field.
async function updateData(db) {
const collection = db.collection('products');
const result = await collection.updateOne(
{ item: 'Mechanical Keyboard' }, // Filter
{ $set: { stock: 50 } } // Update logic
);
console.log('Documents matched:', result.matchedCount);
console.log('Documents updated:', result.modifiedCount);
}
$set operator, MongoDB will replace the entire document with your update object, potentially losing other data fields!
Output:
Modified count: 1
Delete (Remove Data)
Removing data is straightforward. Use a filter to specify which document(s) should be deleted.
async function deleteData(db) {
const collection = db.collection('products');
const result = await collection.deleteOne({ item: 'Mechanical Keyboard' });
console.log('Successfully deleted:', result.deletedCount);
}
find() operation first. This ensures you are deleting exactly what you intended and prevents accidental data loss.
Output:
Deleted count: 1
Step 4 Close the Connection
Database connections are valuable resources. Leaving too many open can lead to "Too many connections" errors and degrade server performance.
async function closeConnection(client) {
// Always close the connection in a 'finally' block
// or at the end of your application script.
await client.close();
console.log('Database connection closed safely.');
}
Summary
Integrating Node.js with MongoDB provides a powerful, high-performance stack for modern development. By using the official driver, you gain full control over your data with an intuitive JavaScript API. We've covered the lifecycle of a database operation: connecting, performing CRUD operations, and managing the connection state. From here, you might explore Mongoose for object modeling or dive into MongoDB's Aggregation Pipeline for advanced data processing.