在mongoose模式中查找嵌套的_id

时间:2013-11-25 07:09:51

标签: mongodb mongoose

模式

question = {
 body: String,
 answer : [ { body: String } ]
}

questionModel.findOne({_id: questionId}, function(err, questionSet){
 var answer1 = {body: 'body1'};
 questionSet.answer.push(answer1);
 questionSet.save(function(err){ console.log(err); });
 // code to find _id of answer1
}

questionSet = {
 _id : ObjectId(_id_q1)
 body: 'String1',
 answer: [ { _id : ObjectId(_id_answer1), body: 'body1'}]
}

我想找到_id_answer1。找到它的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

// code to find _id of answer1
var lastEntryPosition = questionSet.answer.length - 1;
console.log(questionSet.answer[lastEntryPosition]._id);