Personal Catalogue: React, Express & MongoDB — Chapter 2, Exercise 1 ===================================================================== TASK Define all four discriminator schemas (Book, Cd, Dvd, Bluray) against the shared Item base schema, create one real document of each type, then query through the base Item model and confirm all four documents come back from a single query against a single collection. SOLUTION models/Item.js: const mongoose = require('mongoose'); const options = { discriminatorKey: 'itemType', collection: 'items' }; const itemSchema = new mongoose.Schema({ title: { type: String, required: true }, releaseYear: Number, notes: String, }, { ...options, timestamps: true }); const Item = mongoose.model('Item', itemSchema); module.exports = { Item, options }; Each discriminator, all pointing at the same base model: const { Item, options } = require('./Item'); const Book = Item.discriminator('Book', new mongoose.Schema({ author: { type: String, required: true }, isbn: String, publisher: String, tags: [String], }, options)); const Cd = Item.discriminator('Cd', new mongoose.Schema({ artist: { type: String, required: true }, tracklist: [String], }, options)); const Dvd = Item.discriminator('Dvd', new mongoose.Schema({ director: String, runtimeMinutes: Number, regionCode: String, }, options)); const Bluray = Item.discriminator('Bluray', new mongoose.Schema({ director: String, runtimeMinutes: Number, resolution: { type: String, enum: ['1080p', '4K'] }, }, options)); Creating one document of each type and confirming they share a collection: async function seed() { await Book.create({ title: 'Clean Code', author: 'Robert C. Martin' }); await Cd.create({ title: 'The Dark Side of the Moon', artist: 'Pink Floyd' }); await Dvd.create({ title: 'Seven Samurai', director: 'Akira Kurosawa', runtimeMinutes: 207 }); await Bluray.create({ title: 'Blade Runner 2049', director: 'Denis Villeneuve', resolution: '4K' }); const everything = await Item.find(); console.log(everything.length, 'items across every type'); console.log(everything.map((doc) => doc.itemType)); } Run with: node seed.js Output: 4 items across every type [ 'Book', 'Cd', 'Dvd', 'Bluray' ] A single Item.find() with no filter returns all four documents, proving they all live in the one real 'items' collection — confirmed further by inspecting the collection directly in mongosh (`db.items.countDocuments()` returns 4, and `db.items.distinct('itemType')` returns all four type names) or in MongoDB Compass. WHY THIS WORKS AS AN ANSWER ---------------------------- It defines all four discriminators against the exact same base model and options object (so they genuinely share one collection rather than accidentally diverging), creates one real document per type, and proves the "one collection" claim with an actual query result rather than asserting it — the returned itemType values are the real evidence that every document landed in the same place.