0

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?

4
  • Can you share your object ? Paste your complete object. Commented Jul 11, 2015 at 19:50
  • Do you mean the JSON file? Commented Jul 11, 2015 at 19:58
  • yeah, your json object. it will help me to debug Commented Jul 11, 2015 at 19:59
  • I edited my question. However, I'm currently using Postman and MongoDb locally only. Commented Jul 11, 2015 at 20:05

1 Answer 1

0

These seems like a case of one-to-many relationship in mongodb. You can look at the link: one-to-many

You have to create an authors collection and reference it by its id in other collections:

For instance:

{
  _id: 'authors'
  author: [ "Kristina Chodorow", "Mike Dirolf"]
}

In the case of mongoose, you can use ObjectId to refer to Authors inside your Questions schema:

authors: {
  type: Schema.ObjectId, 
  ref: 'authors'
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.