1. Introduction to MongoDB
MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents. It is designed for scalability and high performance.
Key Features:
What is Mongoose?
Mongoose is an ODM (Object Data Modeling) library for MongoDB. It helps structure MongoDB data with schemas and provides powerful tools for query building and data validation.
Why Use Mongoose?
2. Setting Up MongoDB with Mongoose
1. Install Mongoose:
npm install mongoose
2. Connect to MongoDB:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Connection error:', err));
3. Define a Schema:
const userSchema = new mongoose.Schema({ name: String, age: Number });
const User = mongoose.model('User', userSchema);