I'm new to MongoDB and AngularJS and unfortunately I don't know how to search for my problem. I have a schema like this:
var QuestionSchema = new Schema({
"categoryID": Number,
"questionString": String,
"answerA" : String,
"answerB" : String,
"answerC" : String,
"answerD" : String,
"correctAnswer": String,
"author": Number,
"isCertified": {
type: Boolean,
default: false
},
"created": {
type: Date,
default: Date.now
}
});
I then want to display the JSON data with AngularJS in a table:
<tr class="list" ng-repeat="q in questions">
<td class="list">{{ q._id }}</td>
<td class="list">{{ q.categoryID }}</td>
<td class="list">{{ q.questionString }}</td>
<td class="list">{{ q.answerA }}</td>
<td class="list">{{ q.answerB }}</td>
<td class="list">{{ q.answerC }}</td>
<td class="list">{{ q.answerD }}</td>
<td class="list">{{ q.correctAnswer }}</td>
<td class="list">{{ q.author }}</td>
<td class="list">
</tr>
My JSON data looks like this:
{
"questionString": "What is the sense of life?",
"categoryID: "2",
"author": "1",
"answerA": "I am answer A",
"answerB": "I am answer B",
"answerC": "I am answer C",
"answerD": "I am answer D",
"correctAnswer": "3"
}
Although that works, it is not exactly what I want. I want to have another collection for the category and the author so that I could do something like {{ q.author.authorName }}
and {{ q.category.categoryName }}
which should display the author/category name depending on its ID. What is the best practice to store the data like this?
It would be great to define something like this:
var categories = [
{"catID": "1", "catString" : "General"},
{"catID": "2", "catString" : "Games"},
{"catID": "3", "catString" : "Movies"}
];
Another question: would there be a better way to store the answers and the corresponding correctAnswer?