Optimistic concurrency control in MongoDB

763 views
Skip to first unread message

Jeffrey Zhao

unread,
Feb 15, 2011, 10:43:54 PM2/15/11
to mongod...@googlegroups.com
Hello everyone,
 
My scenario needs concurrency control for updates. I found the following content in documents (http://www.mongodb.org/display/DOCS/Atomic+Operations):
 
> t.update({sku:"abc",qty:{$gt:0}}, { $inc : { qty : -1 } } ) ; db.$cmd.findOne({getlasterror:1})
{"err" : , "updatedExisting" : true , "n" : 1 , "ok" : 1} // it worked
> t.update({sku:"abcz",qty:{$gt:0}}, { $inc : { qty : -1 } } ) ; db.$cmd.findOne({getlasterror:1})
{"err" : , "updatedExisting" : false , "n" : 0 , "ok" : 1} // did not work
 
It seems just what I need. But I’ve a question – there’re two separated commands here, what if there’s another update operation (in anther connection) executed between these two, can I still get the result of the first update?
 

Alvin Richards

unread,
Feb 15, 2011, 10:54:13 PM2/15/11
to mongodb-user
So in MongoDB there are no transaction, so each write is atomic but
independent. So between your two statements above, another user could
execute a write.

I'm not sure I understand your question, since if I understand your
example, you have two different SKUs. As the update is executed, the
predicate is evaluated, in you case including the qty, so the update
would not find a document if the update between decreased qty to zero.
If the read occurs between the two operations, the result of the
update on SKU "abc" would be returned.

See also findAndModify, this gives options for what gets returned as
the result of the operation.

http://www.mongodb.org/display/DOCS/findAndModify+Command

-Alvin

Jeffrey Zhao

unread,
Feb 16, 2011, 12:24:57 AM2/16/11
to mongod...@googlegroups.com
What I mean is, after updating a document:

t.update({sku:"abc",qty:{$gt:0}}, { $inc : { qty : -1 } } )

I need to check whether the update is successful or not:
db.$cmd.findOne({getlasterror:1})

But if someone update another document (in another connection) before the
second query, can I still know whether the first update is successful, or
the result of "db.$cmd.findOne({getlasterror:1})" is overwritten.

Actually I'm trying to implement optimistic concurrency control. In RDBMS I
can simply run the sql as below:
UPDATE TableName SET ColumnName = value WHERE Version = old_version

and then check "how many rows effected". But in MongoDB, it seems we cannot
get the number...

-----原始邮件-----
From: Alvin Richards
Sent: Wednesday, February 16, 2011 11:54 AM
To: mongodb-user
Subject: [mongodb-user] Re: Optimistic concurrency control in MongoDB

http://www.mongodb.org/display/DOCS/findAndModify+Command

--
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mongodb-user?hl=en.

Nat

unread,
Feb 16, 2011, 12:27:28 AM2/16/11
to mongod...@googlegroups.com
Getlasterror is connection depentdent command. As long as you do it on the same connection, you could get the error on the previous command executed on that connection.

Jeffrey Zhao

unread,
Feb 16, 2011, 12:30:01 AM2/16/11
to mongod...@googlegroups.com
That's just what I need. Thank you!

Jeffrey Zhao
Blog: http://blog.zhaojie.me/
Twitter: http://twitter.com/jeffz_cn
-----原始邮件-----

From: Nat
Sent: Wednesday, February 16, 2011 1:27 PM

Jeffrey Zhao

unread,
Feb 16, 2011, 1:10:39 AM2/16/11
to mongod...@googlegroups.com
It seems that the update by findAndModify doesn't put
"{updatedExisting:true}" in the result of
"db.$cmd.findOne({getlasterror:1})"?

-----原始邮件-----
From: Alvin Richards
Sent: Wednesday, February 16, 2011 11:54 AM
To: mongodb-user
Subject: [mongodb-user] Re: Optimistic concurrency control in MongoDB

So in MongoDB there are no transaction, so each write is atomic but

http://www.mongodb.org/display/DOCS/findAndModify+Command

--

Eliot Horowitz

unread,
Feb 16, 2011, 1:18:30 AM2/16/11
to mongod...@googlegroups.com
after a findAndModify getLastError returns for the command, not the update.
the data you need should be in the result of the findAndModify command.

Scott Hernandez

unread,
Feb 16, 2011, 2:14:16 AM2/16/11
to mongod...@googlegroups.com
Here is a write up I did about optimistic concurrency (in java):
http://code.google.com/p/morphia/wiki/MongoNewsletterArticleDec2010

This is one of the features in morphia.

Reply all
Reply to author
Forward
0 new messages