mongodb,更新后得到结果行

我正在使用mongodb,node.js和socket.io,并试图减less访问数据库的数量。 我需要更新一行; 并在返回更新后的行这是我是如何做到的:

db.collection('users').update({_id:targetID}, {$set: { 'property': 'value' }}, {safe:true}, function(err, result) { db.collection('users').find({_id:targetID}).toArray(function(error, results){ //a socket.io resend the content }); }); 

它的工作,但我真的倒下,像我在这里有一个无用的步骤。 更新函数的callback似乎是一个布尔值。

顺便说一句,有没有比这更好的文档: http : //docs.mongodb.org/manual/applications/update/ ? 我想find属性和方法的列表。 例如{safe:true} 。 它似乎不工作没有它,但我不能在参考中find它。

也许我完全错了,这不是我应该这样做的方式。 如果你有更好的主意…… 🙂

你可以使用findAndModify来有效地做到这一点:

 db.collection('users').findAndModify( {_id: targetID}, [], {$set: { 'property': 'value' }}, {new: true}, // Return the updated doc rather than the original function(err, result) { // result contains the updated document } );