MongoDB in Go (golang) with mgo: How do I update a record, find out if update was successful and get the data in a single atomic operation?
Posted
by
Sebastián Grignoli
on Stack Overflow
See other posts from Stack Overflow
or by Sebastián Grignoli
Published on 2012-07-10T16:30:43Z
Indexed on
2012/09/10
15:38 UTC
Read the original article
Hit count: 308
I am using mgo driver for MongoDB under Go.
My application asks for a task (with just a record select in Mongo from a collection called "jobs") and then registers itself as an asignee to complete that task (an update to that same "job" record, setting itself as assignee).
The program will be running on several machines, all talking to the same Mongo. When my program lists the available tasks and then picks one, other instances might have already obtained that assignment, and the current assignment would have failed.
How can I get sure that the record I read and then update does or does not have a certain value (in this case, an assignee) at the time of being updated?
I am trying to get one assignment, no matter wich one, so I think I should first select a pending task and try to assign it, keeping it just in the case the updating was successful.
So, my query should be something like:
"From all records on collection 'jobs', update just one that has asignee=null, setting my ID as the assignee. Then, give me that record so I could run the job."
How could I express that with mgo driver for Go?
© Stack Overflow or respective owner