Madhusudan Joshi
Madhusudan Joshi

Reputation: 4476

Concurrent update to the same collection same document

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??

Upvotes: 1

Views: 2003

Answers (1)

evanchooly
evanchooly

Reputation: 6233

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.

Upvotes: 2

Related Questions