1
findAndModify
在mongodb中是很棒的,但是我知道我修改了哪個嵌入式文檔,有點麻煩。Mongodb findAndModify嵌入式文檔 - 你怎麼知道你修改了哪一個?
這裏是一個例子,其中一個Post
embeds_many Comments
。 (我使用的是Mongoid ORM,但問題對於任何MongoDB設置都是通用的)。
begin
p = Post.asc(id).where(comments: { '$elemMatch' => {reserved: false} }).find_and_modify({'$set' => {'comments.$.reserved' => true}}, {new: true}
# now i need to find which comment I just reserved
c = p.comments.select{|c| c.reserved }.first
...
ensure
c.update_attribute :reserved, false
end
好這類作品,但如果我同時運行多個這個過程我select
可以選擇的評價是另一個進程已保留(競爭狀態)。
這是最接近我現在(通過進程ID保留):
begin
p = Post.asc(id).where(comments: { '$elemMatch' => {reserved: nil} }).find_and_modify({'$set' => {'comments.$.reserved' => Process.pid}}, {new: true}
# now i need to find which comment I just reserved
c = p.comments.select{|c| c.reserved == Process.pid }.first
...
ensure
c.update_attribute :reserved, nil
end
這似乎工作。這是做這件事的最好方式還是有更好的模式?
我不太明白他們如何成爲競爭條件 - findAndModify是原子。你可以發佈一個示例文件 - 也許你會更加清楚你擔心的情況。 –
@BrianArmstrong是否面臨競爭條件或它的假設,因爲Asya說findAndModify是和原子操作 – Viren