在参考文档中插入数据?
Inserting data in Referenced Documents?
我正在尝试将数据插入到 myMongoDB 数据库中,我正在使用 Referenced Documents,但我找不到任何方便的方法来插入数据(虽然获取非常方便)。
请在标记重复之前阅读它,我在发布之前搜索了整个 Whosebug 和 github
这是我的考试大纲
const ExamSchema = new mongoose.Schema({
name: {},
description: {},
allowedTime: {},
subject: {},
assignedInCharge: {},
questions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'MCQuestion' }]
});
下面是我的问题架构:
const MultipleChoiceQuestionSchema = new mongoose.Schema({
question: {},
answerOptions: [{}],
correctAnswer: {},
marksForCorrectAnswer: {},
negativeMark: {},
difficulty: {}
});
我的问题是:mongoose 中是否有任何内置方法可以将我在文档中丢失的数据放入数据库(我已经在那里搜索了一千次)。
我的输入如下:
{
"name": "Sample Exam 1",
"description": "IDK if this will work",
"allowedTime": 123445666,
"subject": "testsub1",
"assignedInCharge": "someincharge",
"questions": [
{
"question": "this is que1",
"answerOptions": ["this is op 1", "this is op 2", "op 3", "op 4"],
"correctAnswer": "this is op 1",
"marksForCorrectAnswer": 4,
"negativeMark": 1,
"difficulty": 3
},
{}, {}, {}
]
}
而且(很抱歉有很长的片段)我试图将数据插入数据库的方法是:
router.post('/admin/createExam', (request, response) => {
questions = request.body.questions;
var idarray = [];
questions.forEach(function(element) {
var question = new MCQuestion(element);
question.save().then((doc) => idarray.push(doc._id), (error) => console.log(error));
});
var body = _.pick(request.body, ['Everything Except Questions']);
body.questions = idarray;
var exam = new Exam(body);
exam.save().then(() =>{
response.send(exam);
}).catch((error) => {
response.status(400).send(error);
});
});
我能够成功地将问题保存到问题集合中,但是考试集合没有正确保存。
The Questions collection
The Exam Collection
请帮帮我,几个星期以来我一直在纠结这个问题。
没关系,我想通了,改变 API 方法使它变得很好
router.post('/admin/createExam', (request, response) => {
var body = _.pick(request.body, ['name', 'description', 'allowedTime', 'subject', 'assignedInCharge']);
var exam = new Exam(body);
request.body.questions.forEach(function(element) {
element.exam = exam._id;
var question = new MCQuestion(element);
exam.questions.push(question._id);
question.save().catch((error) => console.log(error));
});
exam.save().then(() =>{
response.send(exam);
}).catch((error) => response.status(400).send(error));
});
我正在尝试将数据插入到 myMongoDB 数据库中,我正在使用 Referenced Documents,但我找不到任何方便的方法来插入数据(虽然获取非常方便)。
请在标记重复之前阅读它,我在发布之前搜索了整个 Whosebug 和 github
这是我的考试大纲
const ExamSchema = new mongoose.Schema({
name: {},
description: {},
allowedTime: {},
subject: {},
assignedInCharge: {},
questions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'MCQuestion' }]
});
下面是我的问题架构:
const MultipleChoiceQuestionSchema = new mongoose.Schema({
question: {},
answerOptions: [{}],
correctAnswer: {},
marksForCorrectAnswer: {},
negativeMark: {},
difficulty: {}
});
我的问题是:mongoose 中是否有任何内置方法可以将我在文档中丢失的数据放入数据库(我已经在那里搜索了一千次)。
我的输入如下:
{
"name": "Sample Exam 1",
"description": "IDK if this will work",
"allowedTime": 123445666,
"subject": "testsub1",
"assignedInCharge": "someincharge",
"questions": [
{
"question": "this is que1",
"answerOptions": ["this is op 1", "this is op 2", "op 3", "op 4"],
"correctAnswer": "this is op 1",
"marksForCorrectAnswer": 4,
"negativeMark": 1,
"difficulty": 3
},
{}, {}, {}
]
}
而且(很抱歉有很长的片段)我试图将数据插入数据库的方法是:
router.post('/admin/createExam', (request, response) => {
questions = request.body.questions;
var idarray = [];
questions.forEach(function(element) {
var question = new MCQuestion(element);
question.save().then((doc) => idarray.push(doc._id), (error) => console.log(error));
});
var body = _.pick(request.body, ['Everything Except Questions']);
body.questions = idarray;
var exam = new Exam(body);
exam.save().then(() =>{
response.send(exam);
}).catch((error) => {
response.status(400).send(error);
});
});
我能够成功地将问题保存到问题集合中,但是考试集合没有正确保存。
The Questions collection
The Exam Collection
请帮帮我,几个星期以来我一直在纠结这个问题。
没关系,我想通了,改变 API 方法使它变得很好
router.post('/admin/createExam', (request, response) => {
var body = _.pick(request.body, ['name', 'description', 'allowedTime', 'subject', 'assignedInCharge']);
var exam = new Exam(body);
request.body.questions.forEach(function(element) {
element.exam = exam._id;
var question = new MCQuestion(element);
exam.questions.push(question._id);
question.save().catch((error) => console.log(error));
});
exam.save().then(() =>{
response.send(exam);
}).catch((error) => response.status(400).send(error));
});