Database Reference
In-Depth Information
NOTE
If, for some reason, you need to determine whether an upsert was an insert or an update,
you can always check the result of the update operation:
>>>
>>> result = db . foo . update ({ 'x' : 15 }, { '$set' : { 'y' : 5 } }, upsert = True )
>>>
>>> result [ 'updatedExisting' ]
False
>>>
>>> result = db . foo . update ({ 'x' : 15 }, { '$set' : { 'y' : 6 } }, upsert = True )
>>>
>>> result [ 'updatedExisting' ]
True
Pre-allocate
To prevent document growth, we'll pre-allocate new documents before the system needs
them. In pre-allocation, we set all values to 0 so that documents don't need to grow to accom-
modate updates. Consider the following function:
def
def preallocate ( db , dt_utc , site , page ):
# Get id values
id_daily = dt_utc . strftime ( '%Y%m %d /' ) + site + page
id_monthly = dt_utc . strftime ( '%Y%m/' ) + site + page
# Get daily metadata
daily_metadata = {
'date' : datetime . combine ( dt_utc . date (), time . min ),
'site' : site ,
'page' : page }
# Get monthly metadata
monthly_metadata = {
'date' : daily_m [ 'd' ] . replace ( day = 1 ),
'site' : site ,
'page' : page }
# Initial zeros for statistics
daily_zeros = [
( 'hourly. %d ' % h , 0 ) for
for i iin range ( 24 ) ]
daily_zeros += [
( 'minute. %d . %d ' % ( h , m ), 0 )
for
for h iin range ( 24 )
for
for m iin range ( 60 ) ]
Search WWH ::




Custom Search