用Mongooseembedded文档

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

这是架构:

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] }); 

我正在尝试创build一个允许用户input一个问题和一些答案的表单(我正在使用express和jade)。

以下是我到目前为止:

 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'); }); }); 

这工作很好,但带我到我的问题…我怎么会在这种forms添加答案?

(或更普遍的问题,我将如何把这样的forms的embedded式文件?)

我试着用谷歌search,看了很多的例子,我没有碰到这个,谢谢你看看。

你可以像下面这样将答案“推”到答案数组中:

  question.answers.push( { answer: "an answer here" });