Mongooseのドキュメントを見ると
If you want to update a single document in the db and return it to your application, use findOneAndUpdate instead.
とあるので、もし req.body.id でドキュメントがユニークになるというのであれば、findOneAndUpdate()を使うのがいいのかもしれません
JavaScript
1Hoge.findOneAndUpdate(
2 {id: req.body.id},
3 {$addToSet:
4 { message: req.body.message }
5 },
6 {upsert:false, new: true},
7 function(err, updatedDocument){
8 if (err) {
9 logger.error(err);
10 res.status(400).end('Error!');
11 }
12 // もし更新したドキュメントがあるなら
13 // updatedDocumentに更新後のドキュメントが入っている
14 }
15);
upsert防止のfalseに加えて、
new: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
とあるとおり new: trueを指定してあげるのがポイントです
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/14 05:46