This page shows a update reaching right into a formerly retrieved (find) document and querying a subelement (array) to update it. I virtually have to do the identical factor. Code for that example:

> t.find()
{ "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
  "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }

> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )

Do you know the rules regulating find-adopted-by-update, I've not observed a reason with this within the documentation. Does exactly the same stuff affect utilization of mongodb via motorists ? A hyperlink towards the relevant semantics could be useful. I'm while using C++ driver.

edit: self answer

the two instructions could be folded into one (and this is why of getting rid of the ambiguity this boosts), the query a part of an update can make reference to a array sub-element, and also the $ symbol will mention of the it. I suppose you are able to only reference one sub-aspect in the query a part of an update operation. During my situation the update operation looks the following :

db.qrs.update ( { "_id" : ObjectId("4f1fa126adf93ab96cb6e848"), "urls.u_id" : 171 }, { "$inc" :  { "urls.$.CC": 1} })

The _id properly "primes" the best unique row, and also the second query element "urls.u_id" : 171 assures the row under consideration has got the right area. urls.$.CC then routes the $inc operation towards the correct array entry.

recomendation to the mongodb dev or document author

Don't show good examples that have potential race conditions inside them. Always avoid showing multiple procedures that you can do atomically.

The guidelines are relatively straightforward. The outcomes from the update might be accessible to the subsequent reads based on numerous things (slaveOk true/false in conjunction with repsets, update and discover using different connections, write safety). You are able to guarantee so that it is available should you perform a safe write (w >= 1) and carry out the find on a single connection. Most motorists offer functionality with this (typically "requestStart" and "requestDone").

All nevertheless, there's a far greater solution open to you with this, namely findAndModify. This operation finds a document, updates it and returns either that old version from the document or even the recently up-to-date version. This command will come in the C++ driver. For any reference take a look here : http://www.mongodb.org/display/DOCS/findAndModify+Command

EDIT : Please be aware the "find" within the example is just there to exhibit the readers from the documentation exactly what the structure/schema from the documents within the collection would be to put the subsequent "update" in context. The "update" operation is definitely impacted by the "find" before it.