【问题标题】:MongoDB nested object array post queryMongoDB 嵌套对象数组后查询
【发布时间】:2016-09-05 13:53:45
【问题描述】:

我无法向 MongoDB 插入数据。我有以下代码。我尝试了许多类似的答案,但对我没有用。

架构 - question.js

var questionsSchema = mongoose.Schema({
question: String,
quizdetails: String,
answers: {id:{type: String},text: { type: String }},
correctanswer: String,
feedback: String
});

路由器 - questions.js

router.post('/',jsonParser, function(req, res, next) {
 console.log(req.body);
}

退货

{ question: 'quiz title',
  quizDetails: 'quiz description',
  answers:
   { '0': 'Answer 1',
     '1': 'Answer 2',
     '2': 'Answer 3',
     '3': 'Answer 4' },
  correctAnswer: 2,
  quizFeedback: 'Feedback' }

回调

module.exports.createQuestion = function(newQuestion, callback) {
  newQuestion.save(callback);
}

【问题讨论】:

标签: javascript node.js mongodb express


【解决方案1】:

我会将答案类型设为数组:

answers: Array

然后返回文档应该是(我们将在集合中插入的文档)

{ question: 'quiz title',
  quizDetails: 'quiz description',
  answers: ['Answer 1',
            'Answer 2',
            'Answer 3',
            'Answer 4'],
  correctAnswer: 2,
  quizFeedback: 'Feedback' }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2018-11-01
    • 2019-05-17
    • 2018-09-10
    • 2020-03-28
    相关资源
    最近更新 更多