Node.js mongodb,更新后获取结果行

Node.js mongodb,更新后获取结果行,node.js,mongodb,socket.io,Node.js,Mongodb,Socket.io,我正在使用mongodb、node.js和socket.io,并试图减少访问数据库的次数。我需要更新一行;在返回更新的行之后,我是这样做的: db.collection('users').update({_id:targetID}, {$set: { 'property': 'value' }}, {safe:true}, function(err, result) { db.collection('users').find({_id:targetID}).toArray(functio

我正在使用mongodb、node.js和socket.io,并试图减少访问数据库的次数。我需要更新一行;在返回更新的行之后,我是这样做的:

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
    });
});
它起作用了,但我真的觉得我在这没用了。update函数的回调似乎是一个布尔值

顺便问一下,有没有比这个更好的文档:?我想查找属性和方法的列表。例如,
{safe:true}
。没有它似乎不起作用,但我在参考资料中找不到它

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

您可以使用来高效地执行此操作:

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
    }
);
您可以使用来高效地执行此操作:

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

你是我的救世主!我不知道如何阅读文档。。。findAndModify和findAndRemove将很快帮助我:)你是我的救世主!我不知道如何阅读文档。。。findAndModify和findAndRemove对我很有帮助:)你所说的“安全:正确”似乎不起作用是什么意思?我写道:“没有它似乎不起作用”。所以,是的,它在工作,但我不知道它是做什么的。你说的“安全:真实”似乎不起作用是什么意思?我写道:“没有它,它似乎不起作用。”。所以,是的,它正在工作,但我不知道它做什么。