我是MongoDB和AngularJS的新手,不幸的是我不知道如何搜索我的问题。 我有这样的架构:
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
}
});
然后,我想在表格中使用AngularJS显示JSON数据:
<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>
我的JSON数据如下所示:
{
"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"
}
虽然这有效,但这并不是我想要的。我希望为该类别和作者提供另一个集合,以便我可以执行类似{{ q.author.authorName }}
和{{ q.category.categoryName }}
的操作,这些内容应根据其ID显示作者/类别名称。存储数据的最佳做法是什么?
定义这样的东西会很棒:
var categories = [
{"catID": "1", "catString" : "General"},
{"catID": "2", "catString" : "Games"},
{"catID": "3", "catString" : "Movies"}
];
另一个问题:是否有更好的方法来存储答案和相应的正确答案?
答案 0 :(得分:0)
这些似乎就像mongodb中的一对多关系一样。您可以查看链接:one-to-many
您必须创建一个authors集合,并通过其在其他集合中的id引用它:
例如:
{
_id: 'authors'
author: [ "Kristina Chodorow", "Mike Dirolf"]
}
对于mongoose,您可以使用ObjectId来引用问题模式中的作者:
authors: {
type: Schema.ObjectId,
ref: 'authors'
}