I am a little confused about the return value of Mongoldb update and how should I handle error with it.
I am using Node.js, Express.js and Mongoose.js as my Mongodb driver
As I look through many tutorial, the only way of error handling I saw is ...
Example: A simple user schema .. and I want to update telephoneNumber
Users
{
email : [email protected],
telephoneNumber : 123456
}
Example of error handling written in node.js that many tutorial taught me
Users.update({email: [email protected]}, {'$set': {telephoneNumber : 654321}, function(err, result){
if(err){
//err
}else if(!result){
//update not success
}else{
//update success
}
});
but as I look through Mongodb documentation, I found out that update return WriteConcern value, which return something like this
{
"ok" : 1, // update with no err
"nModified" :1, // successfully update 1 user
"n" : 1 // found 1
}
So my question is, should I handle my error like this instead, so I would know more about the failures of update...
Users.update({email: [email protected]}, {'$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
}
});
Is this a bad approach to update handling in mongoose/mongodb?
Thanks!! :)