Creating MongoDB Schema for CloudNoteBook App | React JS Tutorial for Beginners ๐๏ธ๐
So far in this React JS tutorial series, we've been building the frontend of our
CloudNoteBook App. Now it's time to move to the backend and set up our database.
Before we can save or fetch any notes, we need a clear, well-structured MongoDB schema
that defines exactly what a "note" looks like โ its title, description, tag, and the date
it was created.
In this MongoDB & Mongoose tutorial for beginners, we will learn:
Mongoose in a Node.js backendNotes schema with fields and data typesWhat is a MongoDB Schema?
MongoDB is a NoSQL database that stores data as flexible, JSON-like documents. Even though MongoDB doesn't force a fixed structure the way SQL databases do, we still want consistency โ every note in our CloudNoteBook App should have the same shape. That's exactly what Mongoose (an ODM โ Object Data Modeling library for MongoDB and Node.js) gives us: a schema that defines the fields, their data types, and validation rules for every document in a collection.
---Step 1: Install Mongoose
Inside your backend project folder, install mongoose using npm.
npm install mongoose
Step 2: Connect to MongoDB
Create a db.js file in your backend root folder. This file will hold the logic
to connect our Node.js server to a local or cloud-hosted (MongoDB Atlas) database.
const mongoose = require("mongoose");
const mongoURI = "mongodb://127.0.0.1:27017/cloudnotebook";
const connectToMongo = () => {
mongoose.connect(mongoURI)
.then(() => {
console.log("Connected to MongoDB successfully โ
");
})
.catch((err) => {
console.error("MongoDB connection error โ", err);
});
};
module.exports = connectToMongo;
Step 3: Create a models Folder
It's a good practice to keep all your Mongoose schemas inside a dedicated
models folder. Create a new file called Notes.js inside it.
backend/
โโโ models/
โ โโโ Notes.js
โโโ db.js
โโโ index.js
Step 4: Define the Notes Schema
Now let's define the actual structure of a "note" using mongoose.Schema.
Each note will have a title, description, an optional tag,
and a date that defaults to the current time.
const mongoose = require("mongoose");
const { Schema } = mongoose;
const NotesSchema = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
title: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
tag: {
type: String,
default: "General",
},
date: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("notes", NotesSchema);
Step 5: Understanding Each Field
Let's break down why each field is defined the way it is, and what role it plays when we later build the Add/Edit Note APIs for our CloudNoteBook App.
| Field | Type | Purpose |
|---|---|---|
user |
ObjectId (ref: "user") |
Links each note to the specific user who created it |
title |
String, required |
The heading of the note; cannot be empty |
description |
String, required |
The main content/body of the note |
tag |
String, default "General" |
Helps categorize notes (e.g. Work, Personal, Ideas) |
date |
Date, default Date.now |
Automatically records when the note was created |
Step 6: Connect Everything in index.js
Finally, import and call connectToMongo in your main server file so the
database connection is established as soon as the server starts.
const connectToMongo = require("./db");
const express = require("express");
connectToMongo();
const app = express();
const port = 5000;
app.get("/", (req, res) => {
res.send("Welcome to CloudNoteBook Backend ๐");
});
app.listen(port, () => {
console.log(`CloudNoteBook backend listening on port ${port}`);
});
Features and Learnings:-
mongoose in a Node.js backend.mongoose.connect().models folder.Notes schema with title, description, tag, and date fields.required and default options add basic data validation.ref to the user collection.