Database Reference
In-Depth Information
cart = carted [ cart [ '_id' ]]
db . product . update (
{ '_id' : item [ '_id' ],
'carted.cart_id' : cart [ '_id' ] },
{ '$set' : { 'carted.$.timestamp' : now } })
del
del carted [ cart [ '_id' ]]
# Second Pass: All the carted items left in the dict need to now be
# returned to inventory
for
for cart_id , carted_item iin carted . items ():
db . product . update (
{ '_id' : item [ '_id' ],
'carted.cart_id' : cart_id },
{ '$inc' : { 'qty' : carted_item [ 'qty' ] },
'$pull' : { 'carted' : { 'cart_id' : cart_id } } })
Here, we're visiting each SKU that has possibly expired carted entries one at a
time. This has the potential for being time-consuming, so the timeout value should
be chosen to keep the number of SKUs returned small. In particular, this timeout
value should be greater than the timeout value used when expiring carts.
Note that we're once again using the positional $ to update only the carted item
we're interested in. Also note that we're not just updating the product document
in-memory and calling .save() , as that can lead to race conditions.
Here again we don't call .save() , since the product's quantity may have been up-
dated since this function started executing. Also note that we might end up modi-
fying the same product document multiple times (once for each possibly expired
carted entry). This is most likely not a problem, as we expect this code to be ex-
ecuted extremely infrequently.
Here, the index we need is on carted.timestamp to make the initial find() run quickly:
>>> db . product . ensure_index ( 'carted.timestamp' )
 
 
 
 
Search WWH ::




Custom Search