fab*_*ien 1 mongodb node.js 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(function(error, results){
//a socket.io resend the content
});
});
Run Code Online (Sandbox Code Playgroud)
它有效,但我真的感觉像是在这里做了一个无用的步骤.更新函数的回调似乎是一个布尔值.
顺便说一下,有没有比这个更好的文档:http://docs.mongodb.org/manual/applications/update/?我想找一个属性和方法列表.在{safe:true}为实例.没有它似乎没有用,但我在参考文献中找不到它.
也许我完全错了,这不是我应该这样做的方式.如果你有更好的主意...... :)
您可以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
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1965 次 |
| 最近记录: |