我想做一个投票应用程序,我有一个架构称为投票。 在调查模式我有一个“选项”对象。 我想通过id更新options.vote。 我打电话给民意调查(id).options(id).vote? 我尝试:Mongoose和mongodb findById与对象在对象
app.post("/:id/:option_id", function(req, res){
Poll.findById(req.params.id,function(err, poll){
if(err){
console.log("Vote(find the Poll) post err");
} else{
poll.options.findById(req.params.option_id,function(err,option){
if(err){
console.log("Vote(find the option) post err");
} else{
option.vote++;
option.vote.save(function(err){
if(err){
console.log("save vote error");
} else{
res.redirect("/:id/:option_id");
}
});
}});
}
你想'.findOne()'作为'.findById()'是“严格地说”'.findOne({“_id”:id})的别名快捷方式''。就是这样。你想要别的东西,那么你可以使用完整的方法。 –