我对 Mongodb update 的返回值以及如何处理它的错误有点困惑。
我使用 Node.js、Express.js 和 Mongoose.js 作为我的 Mongodb 驱动程序
当我浏览许多教程时,我看到的错误处理的唯一方法是......
示例:一个简单的用户架构..并且我想更新电话号码
Users
{
email : [email protected] /cdn-cgi/l/email-protection,
telephoneNumber : 123456
}
许多教程教我的用 Node.js 编写的错误处理示例
Users.update({email: [email protected] /cdn-cgi/l/email-protection}, {'$set': {telephoneNumber : 654321}, function(err, result){
if(err){
//err
}else if(!result){
//update not success
}else{
//update success
}
});
但是当我查看 Mongodb 文档时,我发现 update 返回 WriteConcern 值,它返回类似这样的内容
{
"ok" : 1, // update with no err
"nModified" :1, // successfully update 1 user
"n" : 1 // found 1
}
所以我的问题是,我是否应该像这样处理我的错误,这样我就可以了解更多有关更新失败的信息......
Users.update({email: [email protected] /cdn-cgi/l/email-protection}, {'$set': {telephoneNumber : 654321}, function(err, result){
if(err || result.ok === 0){
//err
}else if(result.nModified === 0){
//update fail
}else if(result.n === 0){
//could not be found
}else{
//update success
}
});
这是 mongoose/mongodb 中更新处理的糟糕方法吗?
谢谢!! :)