title: 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?
body: <p>I am using mgo driver for MongoDB under Go.</p>
  <p>My application asks for a task (with just a record select in Mongo from a collection called &quot;jobs&quot;) and then registers itself as an assignee to complete that task (an update to that same &quot;job&quot; record, setting itself as assignee).</p>
  <p>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.</p>
  <p>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?</p>
  <p>I am trying to get one assignment, no matter which 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.</p>
  <p>So, my query should be something like:</p>
  <p>&quot;From all records on collection 'jobs', update <strong>just one</strong> that has assignee=null, setting my ID as the assignee.  <strong>Then, give me that record</strong> so I could run the job.&quot;</p>
  <p>How could I express that with mgo driver for Go?</p>