How to reference to a schema inside the same collection?

Viewed 214

I'm trying to store forms' questions and answers in the same collection. Here's the Model and schemas I've designed:

const mongoose = require("mongoose");

const questionSchema = mongoose.Schema({
  type: String,
  title: String,
});

const answerSchema = mongoose.Schema({
  question: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Form.questions",
  },
  answer: String,
});

const Form = mongoose.model("Form", {
  title: { type: String, default: "Titre du formulaire" },
  questions: [questionSchema],
  answers: [[answerSchema]],
});

module.exports = Form;

For each answer, I'm trying to reference the objectId of the question, but mongoose.Schema.Types.ObjectId seems to work only when referencing to another collection, not to some other id in the same collection. I don't know what to put instead of ref: "Form.questions" to get populate() to display the question in the answer.

Am I on the right track, how can I connect properly an answer to its question? Or do I need to create a separate collection for the answers?

Here's an example of the output:

{
    "title": "Formulaire de test",
    "answers": [
        [
            {
                "_id": "5fd8f8938a111d3a1df9f3bd",
                "question": "5fd8a65ae252ae5b903d4593",
                "answer": "Question 1"
            },
            {
                "_id": "5fd8f8938a111d3a1df9f3be",
                "question": "5fd8a65ae252ae5b903d4595",
                "answer": "Question 3"
            },
            {
                "_id": "5fd8f8938a111d3a1df9f3bf",
                "question": "5fd8a65ae252ae5b903d4594",
                "answer": "Question 2"
            },
            {
                "_id": "5fd8f8938a111d3a1df9f3c0",
                "question": "5fd8f6898a111d3a1df9f3bb",
                "answer": "Question 4"
            },
            {
                "_id": "5fd8f8938a111d3a1df9f3c1",
                "question": "5fd8f6898a111d3a1df9f3bc",
                "answer": "Question 5"
            }
        ],
        [
            {
                "_id": "5fd8f96a9d75193ecd410464",
                "question": "5fd8a65ae252ae5b903d4593",
                "answer": "Question 1"
            },
            {
                "_id": "5fd8f96a9d75193ecd410465",
                "question": "5fd8a65ae252ae5b903d4595",
                "answer": "Question 3"
            },
            {
                "_id": "5fd8f96a9d75193ecd410466",
                "question": "5fd8a65ae252ae5b903d4594",
                "answer": "Question 2"
            },
            {
                "_id": "5fd8f96a9d75193ecd410467",
                "question": "5fd8f6898a111d3a1df9f3bb",
                "answer": "Question 4"
            },
            {
                "_id": "5fd8f96a9d75193ecd410468",
                "question": "5fd8f6898a111d3a1df9f3bc",
                "answer": "Question 5"
            }
        ]
    ],
    "_id": "5fd8f4c5600d65378596748a",
    "questions": [
        {
            "_id": "5fd8a65ae252ae5b903d4593",
            "title": "Question 1",
            "type": "text"
        },
        {
            "_id": "5fd8a65ae252ae5b903d4595",
            "title": "Question 3",
            "type": "text"
        },
        {
            "_id": "5fd8a65ae252ae5b903d4594",
            "title": "Question 2",
            "type": "score"
        },
        {
            "_id": "5fd8f6898a111d3a1df9f3bb",
            "title": "Question 4",
            "type": "text"
        },
        {
            "_id": "5fd8f6898a111d3a1df9f3bc",
            "title": "Question 5",
            "type": "text"
        }
    ],
    "__v": 3
}
0 Answers
Related