2011-08-04 4 views
2

질문과 가능한 답변을 저장하는 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] 
}); 

(내가 표현과 옥을 사용하고 있습니다) 사용자가 질문 몇 가지 답변을 입력 할 수 있습니다 :

다음은 스키마입니다.

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

이 잘 작동하지만 내 질문에 날 리드 ... 는 어떻게 추가 할 : 나는 그것을 저장하는 방법

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

그리고 여기 : 여기

내가 지금까지 가지고 무엇을 이 양식의 답은 무엇입니까?

(또는 더 일반적인 질문, 나는이 같은 형태로 포함 된 문서? 넣어 얼마나) 내가 주변에 인터넷 검색 및 예제를 많이보고 시도

을 나는이로 실행하지 않은, 감사 보세요.

답변

2

할 수 있습니다이 같은 답변 배열에 "푸시"답변 :

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