premise : update claims are harmless because the driver automatically works in a single way texting (as lengthy as getLastError is not used).

question May be the following fragment the easiest method to do that in mongodb for top volume card inserts ? Can you really fold step two and three ?

edit : old buggy form , see below

// step 1 : making sure the top-level document is present (an upsert in the real 
            example)
db.test.insert( { x :1} )

// step 2 : making sure the sub-document entry is present
db.test.update( { x:1 }, { "$addToSet" : { "u" : { i : 1, p : 2 } } }, false)

// step 3 : increment a integer within the subdocument document
db.test.update( { x : 1, "u.i" : 1}, { "$inc" : { "u.$.c" : 1 } },false)

I've got a feeling there's not a way from operation 3, because the$ operator requires priming within the query area from the query a part of an update. amirite ? iamrite ?

If this sounds like the easiest method to do things, can one get creative during my code and go nuts with update procedures ?

edit : new form

There is a bug during my logic, thanks Gates. Still wish to fold the updates if at all possible :D

// make sure the top-level entry exists and increase the incidence counter
db.test.update( { x : 1 }, { $inc : { i : 1 } }, true ) --1

// implicetly creates the array
db.test.update(   { x : 1 , u : { $not : { $elemMatch : { i : 1 } } } } , 
                  { $push : { u : { i : 1 , p :2 , c:0} } }) -- 2

db.test.update( { x :1 , "u.i" : 1}, { $inc : { "u.$.c" : 1 } },false) --3

notes : $addToSet isn't helpfull within this situation, because it does a element-smart match, there's not a way to convey what elements in a wide array might be mutable as with C++ OO bitwise comparison parlance

So, the very first factor to notice would be that the $ positional operator is a touch sketchy. It provides extensive "gotchas": it does not play well with upserts, it only affects the very first true match, etc.

To know "folding" of #2 and #3, you have to consider the creation of your instructions:

db.test.insert( { x :1} )
{ x:1 } // DB value

db.test.update( { x:1 }, { "$addToSet" : { "u" : { i : 1, p : 2 } } }, false)
{ x:1, u: [ {i:1,p;2} ] } // DB value

db.test.update( { x : 1, "u.i" : 1}, { "$inc" : { "u.$.c" : 1 } },false)
{ x:1, u: [ {i:1,p:2,c:1} ] } // DB value

In line with the sequence you provided, the entire factor could be folded right into a single update.

If you are only searching to roll together #2 &lifier #3, then you are concerned about matching 'u.i':1 with u.$.c. But you will find some edge cases here you need to clarify.

Give your beginning document function as the following:

{ 
  x:1, 
 u: [ 
      {i:1, p:2},
      {i:1, p:3}  
    ] 
}

Exactly what do you anticipate from running update #3?

As written you receive:

{ 
  x:1, 
 u: [ 
      {i:1, p:2, c:1},
      {i:1, p:3}  
    ] 
}

Is correct? Is the fact that first document legal? (semantically correct)? With respect to the solutions, this might really be an problem of document structure.