2011-08-04 136 views
2

我有一个简单的Mongoose模式,称为Question,它存储一个问题及其可能的答案。答案是一个单独的模式,并作为嵌入式文档存储在问题中。使用Mongoose嵌入文档

这里的模式:

var ResponseSchema = new Schema({}); 

var AnswerSchema = new Schema({ 
    answer    : String 
    , responses   : [ResponseSchema] 
}); 

var QuestionSchema = new Schema({ 
    question   : {type: String, validate: [lengthValidator, "can't be blank."]} 
    , answers    : [AnswerSchema] 
}); 

我试图创建一个表单(我使用的快递和玉),允许用户输入一个问题,一些答案。

这是我到目前为止有:

form(action='/questions', method='post') 
fieldset 
    p 
     label Question 
     input(type='text', name="question[question]") 
div 
    input(type='submit', value='Create Question') 

这里就是我如何保存:

app.post('/questions', function(req, res, next) { 
    var question = new Question(req.param('question')); 
    question.save(function(err) { 
    if (err) return next(err); 

    req.flash('info', 'New question created.'); 
    res.redirect('/questions'); 
    }); 
}); 

这个伟大的工程,但使我对我的问题... 如何将添加这种形式的答案?

(或更一般的问题,我怎么会放这样的形式嵌入文档?)

我试着用搜索引擎周围,看着很多的例子,我没有跑成这样了,谢谢看一看。

回答

2

您可以“推”到答案的答案数组是这样的:

question.answers.push({ answer: "an answer here" }); 
相关问题