Atomicity of mongodb

Source: Internet
Author: User
If two operations conflict with each other during database operations, the results may be abnormal. I have written this code before: user_Utf8Provider.remove ({},{}, function (result) {user_Utf8Provider.insert (user, {}, function (result) {console. log (create Default User) ;};}); on

If two operations conflict with each other during database operations, the results may be abnormal. I have written this code before: user_Utf8Provider.remove ({},{}, function (result) {user_Utf8Provider.insert (user, {}, function (result) {console. log ("create default user") ;};}); on

If two operations conflict with each other during database operations, the results may be abnormal.

I have written such a piece of code before:

User_Utf8Provider.remove ({},{}, function (result) {user_Utf8Provider.insert (user ,{}, function (result) {console. log ("create default user ");});});

The purpose of the above Code is to delete the previous user every time the server is restarted in the development phase, and then create a new user. On my machine, this can be achieved, however, on a colleague's machine, how can I not add new users? I just deleted the previous data and found that database operations were asynchronous, the following method can be used to meet this requirement:

User_Utf8Provider.remove ({}, {safe: true}, function (result) {user_Utf8Provider.insert (user, {safe: true}, function (result) {console. log ("create default user ");});});

Original article address: the atomicity of mongodb. Thank you for sharing it with the original author.

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.