Lesson 18 of 18

Final Project: Build a Database App

Project Overview

Build a complete bookstore application with MongoDB, demonstrating all the concepts you've learned.

  • Schema design with Mongoose models
  • CRUD operations for books, authors, and reviews
  • Aggregation pipeline for statistics
  • Indexing for performance
  • Data validation and error handling
  • Relationships between collections

Data Models

Define the schemas for the bookstore application.

Example
// models/Book.js
const bookSchema = new Schema({
  title: { type: String, required: true, index: true },
  isbn: { type: String, unique: true },
  author: { type: Schema.Types.ObjectId, ref: 'Author', required: true },
  genre: { type: String, enum: ['Fiction', 'Non-Fiction', 'Sci-Fi', 'Fantasy', 'Tech'] },
  price: { type: Number, required: true, min: 0 },
  published: { type: Date },
  rating: { type: Number, min: 0, max: 5, default: 0 },
  reviews: [{
    user: String,
    text: String,
    rating: { type: Number, min: 1, max: 5 },
    date: { type: Date, default: Date.now }
  }]
});

// Virtual for average rating
bookSchema.virtual('avgRating').get(function() {
  if (!this.reviews.length) return 0;
  const sum = this.reviews.reduce((acc, r) => acc + r.rating, 0);
  return (sum / this.reviews.length).toFixed(1);
});

module.exports = model('Book', bookSchema);