Course

Active Lesson: Create Collection Model

In modern applications, personalization and convenience are key. Users love the ability to save or bookmark content they find helpful, making it easy to revisit and organize. Think about platforms like Stack Overflow or Reddit where users can save posts or questions for future reference. That’s exactly what we’re setting up here.

Task 🎯

Your mission is to Create a Collection Model using Mongoose.

How? 🧩

To get started, let’s break down what details we’ll need to store for each collection. Think about the essentials:

  • Author: Who created the collection?
  • Question: Which question has been bookmarked?

Each collection will be tied to a specific user and question, so we need a structure that captures this relationship.

Resources 📖

As usual, you can use the following resources to help you get started

Hint 💡

Hint
monkey

You’re going to design a Collection Model. Here's how to think about it:

What Information Do We Need?

Start by asking, "What do I need to store in the collection?"

You’ll need to store:

  • The user who saved the question: This connects the bookmark to a specific user
  • The question they saved: This is the actual content being saved

What Relationships Should We Set?

You need two relationships in this model:

  • User-Collection Relationship: A collection belongs to a specific user, so you'll reference the User model using the user's ID (author)
  • Question-Collection Relationship: A collection is for a specific question, so you'll reference the Question model using the question's ID (question)

Which Data Types Should You Use?

  • author (User ID): This should be an ObjectId referencing the User model
  • question (Question ID): This should also be an ObjectId referencing the Question model

By using ObjectId, each saved item in a collection is linked to its corresponding user and question document, making data retrieval seamless.

What Fields Should Be Required?

Both author and question should be required. You need a valid user and question to create a collection. This ensures every collection is meaningful and complete.

Should We Include Timestamps?

It’s always a good idea to include timestamps! Mongoose can automatically add createdAt and updatedAt to track when each collection is created or modified.

And that's it!


Finish up the model, and celebrate this addition to your growing toolkit of backend skills!

Loading...

0 Comments

glass-bbok

No Comments Yet

Be the first to share your thoughts and start the conversation.

tick-guideNext Lesson

Create Collection Model