Share
Explore

MONGO DB Lab f24 Sept 26/27

info

Note: The Conclusion of this Lab Series:

After you complete the work in this Document: Carry on to add the Expess JS Front End with:

GiTHub Code Repository:


megaphone

We now know what

MONGO DB is, what a NOSQL database is and
how it works with JSON
how we can use JSON to do Big Data (No Primary Key operations)

Carrying on from Yesterday:

Our Learning Outcomes for today’s Class:

Front ending access to our MONGO Database with an Express JS web server using ROUTES
Creating an Express GET endpoint that performs a query (.find, .findById)
Insert a document
Update a document
Delete a document
Working with more than one collection (example: Employees and Vehicles, Books and Authors, etc)

Men's Wear E-Commerce Emporium: MongoDB Schema Design

## Project Overview

We're building a MongoDB database for a Men's Wear E-Commerce Emporium.
This platform will allow users to browse, purchase, and review men's clothing and accessories.

megaphone

Note: I have provides some of the route js files as learning starters.

Try to complete the route js files which are NOT provided here by yourself.

To compare with my solutions: See these additional lab documents:



Initial Collections
1. **Products** 2. **Categories** 3. **Customers** 4. **Orders** 5. **Reviews**
## Basic Schema Designs
### 1. Products Collection
```javascript const productSchema = new mongoose.Schema({ name: { type: String, required: true }, sku: { type: String, required: true, unique: true }, description: String, price: { type: Number, required: true }, category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' }, size: [String], color: [String], inStock: { type: Boolean, default: true }, images: [String] }, { timestamps: true });
const Product = mongoose.model('Product', productSchema); ```
### 2. Categories Collection
```javascript const categorySchema = new mongoose.Schema({ name: { type: String, required: true }, description: String, parentCategory: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' } });
const Category = mongoose.model('Category', categorySchema); ```
### 3. Customers Collection
```javascript const customerSchema = new mongoose.Schema({ firstName: { type: String, required: true }, lastName: { type: String, required: true }, email: { type: String, required: true, unique: true }, password: { type: String, required: true }, address: { street: String, city: String, state: String, zipCode: String, country: String } }, { timestamps: true });
const Customer = mongoose.model('Customer', customerSchema); ```
### 4. Orders Collection
```javascript const orderSchema = new mongoose.Schema({ customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true }, products: [{ product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' }, quantity: Number, price: Number }], totalAmount: Number, status: { type: String, enum: ['Pending', 'Shipped', 'Delivered'], default: 'Pending' }, shippingAddress: { street: String, city: String, state: String, zipCode: String, country: String } }, { timestamps: true });
const Order = mongoose.model('Order', orderSchema); ```
### 5. Reviews Collection
```javascript const reviewSchema = new mongoose.Schema({ product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true }, customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true }, rating: { type: Number, required: true, min: 1, max: 5 }, comment: String }, { timestamps: true });
const Review = mongoose.model('Review', reviewSchema); ```
Work Steps:
1. Set up a new Node.js project — 2. Install necessary dependencies (mongoose, express, nodemon, etc.) —- 3. Create a connection to your MongoDB database 4. Implement these schemas in separate files 5. Create basic CRUD operations for each collection 6. Test the database operations
This schema design provides a solid foundation for our Men's Wear E-Commerce Emporium. As we progress, we can add more complex features like inventory management, user authentication, and advanced querying.

info

Comprehensive guide for setting up the Men's Wear E-Commerce Emporium project, including all the steps you requested. This guide covers:

Setting up a new Node.js project
Installing necessary dependencies
Creating a connection to MongoDB
Implementing schemas in separate files
Creating basic CRUD operations for each collection
Instructions for testing the database operations
The guide includes detailed code snippets and step-by-step instructions for each part of the setup process. It's designed to be ready to run, allowing students to quickly set up a functional backend for the e-commerce platform.
To get started, students should follow the instructions in order, creating the necessary files and implementing the provided code.
Once everything is set up, they can start the server and use tools like Postman or curl to test the API endpoints.

The Database name in MONGO ATLAS

MONGODB_URI=mongodb+srv://username:password@cluster0.mongodb.net/mens_wear_emporium
Here, mens_wear_emporium would be your database name.

error

Complete Lab Steps for the Men's Wear E-Commerce Emporium

Work Flow Steps:

Set up a Mongo Database
Manually CRUD some records
Setup an Express JS Webserver with FORMS and use ROUTES and form data to CRUD the Mongo database.
Do Predicate Joins.

## 1. Set up a new Node.js project
1. Open your terminal and create a new directory: ```bash mkdir mens-wear-emporium cd mens-wear-emporium ```
2. Initialize a new Node.js project: —- ```bash npm init -y ```

2. Install necessary dependencies —-

Install the required packages: ```bash npm install express mongoose dotenv npm install --save-dev nodemon ```

3. Create a connection to your MongoDB database —-

1. Create a `.env` file in the root directory: ```bash touch .env ```
2. Add your MongoDB connection string to the `.env` file: —- ``` MONGODB_URI=your_mongodb_connection_string_here PORT=3000 ```
3. Create a `server.js` file in the root directory: ```bash touch server.js ```
4. Add the following code to `server.js`: ​ ```javascript const express = require('express'); const mongoose = require('mongoose'); require('dotenv').config();
const app = express(); const PORT = process.env.PORT || 3000;
// Middleware app.use(express.json());
// MongoDB Connection mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true, }) .then(() => console.log('Connected to MongoDB')) .catch((err) => console.error('Error connecting to MongoDB:', err));
// Routes (we'll add these later)
app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); }); ``` done up to here.
## 4. Implement schemas in separate files
1. Create a `models` directory: ```bash mkdir models ```
2. Create separate files for each schema in the `models` directory: ```bash touch
models/product.js
models/category.js
models/customer.js
models/order.js
models/review.js ** ```
3. Implement each schema in its respective file:
`models/product.js`: ```javascript const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({ name: { type: String, required: true }, sku: { type: String, required: true, unique: true }, description: String, price: { type: Number, required: true }, category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' }, size: [String], color: [String], inStock: { type: Boolean, default: true }, images: [String] }, { timestamps: true });
module.exports = mongoose.model('Product', productSchema); ```
`models/category.js`: ```javascript const mongoose = require('mongoose');
const categorySchema = new mongoose.Schema({ name: { type: String, required: true }, description: String, parentCategory: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' } });
module.exports = mongoose.model('Category', categorySchema); ```
`models/customer.js`: ```javascript const mongoose = require('mongoose');
const customerSchema = new mongoose.Schema({ firstName: { type: String, required: true }, lastName: { type: String, required: true }, email: { type: String, required: true, unique: true }, password: { type: String, required: true }, address: { street: String, city: String, state: String, zipCode: String, country: String } }, { timestamps: true });
module.exports = mongoose.model('Customer', customerSchema); ```
`models/order.js`: ```javascript const mongoose = require('mongoose');
const orderSchema = new mongoose.Schema({ customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true }, products: [{ product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' }, quantity: Number, price: Number }], totalAmount: Number, status: { type: String, enum: ['Pending', 'Shipped', 'Delivered'], default: 'Pending' }, shippingAddress: { street: String, city: String, state: String, zipCode: String, country: String } }, { timestamps: true });
module.exports = mongoose.model('Order', orderSchema); ```
`models/review.js`: ```javascript const mongoose = require('mongoose');
const reviewSchema = new mongoose.Schema({ product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true }, customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true }, rating: { type: Number, required: true, min: 1, max: 5 }, comment: String }, { timestamps: true });
module.exports = mongoose.model('Review', reviewSchema); ```
5. Create basic CRUD operations for each collection
1. Create a `routes` directory: ```bash mkdir routes ```
2. Create route files for each model: ```bash touch
routes/products.js
routes/categories.js
routes/customers.js
routes/orders.js
routes/reviews.js //done
3. Implement CRUD operations in each route file.
Here's an example for `routes/products.js`:
```javascript const express = require('express'); const router = express.Router(); const Product = require('../models/product');
// Create a new product router.post('/', async (req, res) => { try { const product = new Product(req.body); await product.save(); res.status(201).json(product); } catch (error) { res.status(400).json({ message: error.message }); } });
// Get all products router.get('/', async (req, res) => { try { const products = await Product.find(); res.json(products); } catch (error) { res.status(500).json({ message: error.message }); } });
// Get a single product router.get('/:id', async (req, res) => { try { const product = await Product.findById(req.params.id); if (!product) return res.status(404).json({ message: 'Product not found' }); res.json(product); } catch (error) { res.status(500).json({ message: error.message }); } });
// Update a product router.patch('/:id', async (req, res) => { try { const product = await Product.findByIdAndUpdate(req.params.id, req.body, { new: true }); if (!product) return res.status(404).json({ message: 'Product not found' }); res.json(product); } catch (error) { res.status(400).json({ message: error.message }); } });
// Delete a product router.delete('/:id', async (req, res) => { try { const product = await Product.findByIdAndDelete(req.params.id); if (!product) return res.status(404).json({ message: 'Product not found' }); res.json({ message: 'Product deleted successfully' }); } catch (error) { res.status(500).json({ message: error.message }); } });
module.exports = router; ```
4. Update `server.js` to use the routes:
```javascript const express = require('express'); const mongoose = require('mongoose'); require('dotenv').config();
const productRoutes = require('./routes/products'); const categoryRoutes = require('./routes/categories'); const customerRoutes = require('./routes/customers'); const orderRoutes = require('./routes/orders'); const reviewRoutes = require('./routes/reviews');
const app = express(); const PORT = process.env.PORT || 3000;
// Middleware app.use(express.json());
// MongoDB Connection mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true, }) .then(() => console.log('Connected to MongoDB')) .catch((err) => console.error('Error connecting to MongoDB:', err));
// Routes app.use('/api/products', productRoutes); app.use('/api/categories', categoryRoutes); app.use('/api/customers', customerRoutes); app.use('/api/orders', orderRoutes); app.use('/api/reviews', reviewRoutes);
app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); }); ```

## 6. Test the database operations

1. Start your server: ```bash npx nodemon server.js
npx is a package runner tool that comes with npm (Node Package Manager) 5.2+ and higher. It allows you to execute npm package binaries without having to install them globally or locally. Here's a breakdown of the command:
npx: This tool is used to execute packages.
nodemon: This is a utility that monitors for any changes in your source code and automatically restarts your server.
server.js: This is the main file of your Node.js application that nodemon will run and monitor.
When you run npx nodemon server.js, here's what happens:
npx checks if nodemon is installed locally in your project's node_modules folder.
If nodemon isn't found locally, npx will temporarily download and cache the latest version of nodemon.
npx then runs nodemon server.js.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.