The story between MongoDB data update and _ID

Source: Internet
Author: User

Today in the docking interface encountered some problems, because in order to facilitate the verification interface to write the right is to the database inside of some of their own data, the beginning of the use of the joy, and later encountered an update error.

For example, our app is a medical app just started to register when the default is the patient, as follows

When a doctor wants to register to make money in this app, you need to register to submit your own information, as follows

And then, according to the truth, will be registered successfully but reported after applying the update to the document {_id:3, ...}, the (immutable) field \ ' _id\ ' was found to the been altered to _id:17 '}

I didn't know what the hell was going on. Later asked and I just came to the colleague (also rookie) said is the database inside the first is manually inserted data, and then update the time _id repeated conflict, then listened to delete a lot of data, finally really can, but I am a rookie I think this is not the essence of the problem.

Then I kept checking the information and then I knew the problem was that I wrote the code with the following problems:

//Doctor certification
router.post ('/doctorauth ', function (req,res) {
Console.log (' Request object: ', req.body);
User.update (
{
_id:req.body.doctor_id,
Nickname:req.body.nickname,
Hospital:req.body.hospital,
address:req.body.address,
License:req.body.license,
Photo:req.body.photo,
Identity:enums.identityPerson.doctor,
State:enums.authState.pass,
Department:req.body.department,
speciality:req.body.speciality
},function (err,doctorauth) {
if (err) throw err;
if (doctorauth.nmodified = = 0 && DOCTORAUTH.N = = 0) {
res.send (errors.e112);
return;
}
Console.log (Doctorauth);
res.send (ERRORS.E0);
})
inside I update to bring _id, to know that MongoDB _id is not updated. Finally find the problem, and then simply modify the code as follows:

//Doctor certification
router.post ('/doctorauth ', function (req,res) {
Console.log (' Request object: ', req.body);
User.update (
{
_id:req.body.doctor_id
},
{
Nickname:req.body.nickname,
Hospital:req.body.hospital,
address:req.body.address,
License:req.body.license,
Photo:req.body.photo,
Identity:enums.identityPerson.doctor,
State:enums.authState.pass,
Department:req.body.department,
speciality:req.body.speciality
},function (err,doctorauth) {
if (err) throw err;
if (doctorauth.nmodified = = 0 && DOCTORAUTH.N = = 0) {
res.send (errors.e112);
return;
}
Console.log (Doctorauth);
res.send (ERRORS.E0);
})

});
and then it's ready. After eating a big loss to find a lesson, will never make such a problem again. Maybe this is not a problem in front of you, but for me this rookie to solve the feeling very happy O (∩_∩) o~~

The story between MongoDB data update and _ID

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.