Home > Back-end >  Get all items from a schema in a schema Mongoose
Get all items from a schema in a schema Mongoose

Time:07-19

So right now, I'm using MongoDB with Mongoose for NodeJS. So right here is my code for my schema, and there is a "quizzes" in it which is another schema:

import { quizSchema } from "./quiz.model.js";
import mongoose from "mongoose";

const sessionSchema = new mongoose.Schema(
  {
    title: { type: String, required: true },
    content: { type: String, required: true },
    quizzes: [quizSchema],
  },
  { versionKey: false }
);

So, what I want to do, is get all of the quizzes inside of that specific session I want. So how exactly do I achieve this? I've tried researching but no joy.

CodePudding user response:

you need to reference it by id like this and you do not have to import the model , mongoose handles that

quizzes: [{ type: Schema.Types.ObjectId, ref: "quizzes" }],

and in controller ; make sure to populate

  • Related