Concurrent update to the same collection same document

北慕城南 提交于 2020-01-05 18:37:19

问题


Problem Statement

How do i handle concurrent update commands on same collection and same document.

Current DB Structure

EDIT - Structure changed

  {
     "userId":1,
     "summary_extra":[
        {
           "some_id":1,
           "names":[
              "one",
              "two"
           ]
        },
        {
           "some_id":2,
           "names":[
              "three",
              "four"
           ]
        }
     ]
  },
  {
     "userId":2,
     "summary_extra":[
        {
           "some_id":1,
           "names":[
              "one",
              "two"
           ]
        },
        {
           "some_id":2,
           "names":[
              "three",
              "four"
           ]
        }
     ]
  }

If one user tries to update at a time then there wont be any problem to update the document. But if there are more than one users(whihc is likely to 10, minimum) then how can i update same document.

What do i do in this scenarios??

N:B Datas are exact, but exact same structure i have.

EDIT : So now i think i can update two different documents in the same collections simultaneously by multiple users??


回答1:


You can include a version field which gets incremented with each update (it'd be one more $inc in your update and a query filter). Then if someone has updated the document away from the version you have, no updates can go through. If you're using Morphia, the @Version annotation takes care of this for you transparently.



来源:https://stackoverflow.com/questions/30193283/concurrent-update-to-the-same-collection-same-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!