MongoDB
MongoDB
What is a Database?
● MongoDB is a NoSQL database, meaning it doesn’t store data in tables like SQL
databases.
● It stores data in JSON-like format called Documents.
● It is scalable, flexible, and widely used in modern web development.
Data Storage: MongoDB stores data in JSON-like BSON documents, while MySQL stores
data in structured tables with rows and columns.
Joins: MongoDB does not support traditional SQL joins but uses embedding and
referencing, whereas MySQL relies on JOIN queries to connect multiple tables.
How MongoDB Works
Instead of tables and rows like MySQL, MongoDB stores data as collections and documents.
If you don’t want to install MongoDB locally, you can use MongoDB Atlas (a cloud-based MongoDB
database).
1. Sign up at https://www.mongodb.com/atlas.
2. Create a free cluster.
3. Get your MongoDB connection string.
What is Mongoose?
Mongoose is a JavaScript library that helps you connect your Node.js application to a
MongoDB database and makes working with the database easier.
Mongoose provides a structured way to interact with MongoDB, making it easier to:
mongoose.connect("mongodb:userName:password@cluster/myDatabase")
A schema is like a blueprint. It tells MongoDB what kind of data you expect.
○ / at the end → If no database name is given, it will connect to the default database.
⚠ Security Warning:
Never expose username & password in your code. Store them in a .env file instead.
await mongoose.connect(MONGO_URI); → This waits for MongoDB to connect successfully.
What is process.exit(1);
If we remove process.exit(1);, the app will keep running, even if MongoDB is not connected.
✔ Validation rules
✔ Default values
🔹 Schemas define the structure of data but do not interact with the database directly.
2⃣ What is a Model in Mongoose?
A Model is a wrapper for the Schema and allows us to interact with the MongoDB collection.
1⃣ Importing Mongoose
Loads the mongoose library so we can use MongoDB in our Node.js app.
Creates a Schema (Blueprint) for how user data should be structured in MongoDB.
name:{
type: String
email:{
type: String,
What it does?
type: String,
We can use User to add, find, update, or delete users in the database.
Understanding the Two Parameters in mongoose.model()
● This defines the structure (fields & rules) for documents in the users collection.
● The userSchema ensures that:
○ The document must have specific fields (e.g., name, email, mobile).
○ Some fields must be unique (email, mobile).
○ Some fields are required (email).
○ The role field can only be 'user' or 'admin' (enum).
5⃣ Exporting the Model
module.exports = User;
Makes User available for use in other files (e.g., server.js).
🔹 req → Represents the incoming request (contains data sent by the user).
🔹 res → Represents the response (data sent back to the client).
🔹 async → Used because we are handling database operations (which take time).
Extract Data from Request
{ name, age, email, mobile } → Destructuring (User-defined) → Extracts data from req.body.
🔹 It does not save the data to the database yet! It just prepares the data.
The { name, age, email, mobile } values are used to build the new user.
newUser.save()
findByIdAndUpdate
✅ It is a Mongoose method used to: