问题描述
问题陈述
如何处理同一集合和同一文档上的并发更新命令.
How do i handle concurrent update commands on same collection and same document.
当前数据库结构
编辑-结构已更改
{
"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"
]
}
]
}
如果一个用户一次尝试更新,则更新文档不会有任何问题.但是,如果有多个用户(可能至少有10个用户),那么我该如何更新同一文档.
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数据精确,但结构却完全相同.
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??
推荐答案
您可以包括一个version字段,该字段随每次更新而递增(在您的更新和查询过滤器中,将增加一个$ inc).然后,如果有人从您拥有的版本中更新了文档,则无法进行任何更新.如果您使用的是Morphia,则@Version
注释会为您透明地进行处理.
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.
这篇关于并发更新到相同的集合相同的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!