Database Reference
In-Depth Information
Using this method, there will be a high probability that each document will already exist be-
fore your application needs to issue update operations. You'll also be able to prevent a regular
spike in activity for pre-allocation, and be able to eliminate document growth.
Retrieving data for a real-time chart
This example describes fetching the data from the above MongoDB system for use in gener-
ating a chart that displays the number of hits to a particular resource over the last hour.
We can use the following query in a
find_one
operation at the Python console to retrieve the
number of hits to a specific resource (i.e.,
/index.html
) with minute-level granularity:
>>>
>>>
db
.
stats
.
daily
.
find_one
(
...
...
{
'metadata'
: {
'date'
:
dt
,
'site'
:
'site-1'
,
'page'
:
'/index.html'
}},
...
{
'minute'
:
1
})
Alternatively, we can use the following query to retrieve the number of hits to a resource over
the last day, with hour-level granularity:
code,sourceCode,pycon
>>>
>>>
db
.
stats
.
daily
.
find_one
(
...
...
{
'metadata'
: {
'date'
:
dt
,
'site'
:
'site-1'
,
'page'
:
'/foo.gif'
}},
...
{
'hourly'
:
1
})
If we want a few days of hourly data, we can use a query in the following form:
>>>
>>>
db
.
stats
.
daily
.
find
(
...
...
{
...
'metadata.date'
: {
'$gte'
:
dt1
,
'$lte'
:
dt2
},
...
'metadata.site'
:
'site-1'
,
...
'metadata.page'
:
'/index.html'
},
...
{
'metadata.date'
:
1
,
'hourly'
:
1
} },
...
sort
=
[(
'metadata.date'
,
1
)])
To support these query operations, we need to create a compound index on the following daily
statistics fields:
metadata.site
,
metadata.page
, and
metadata.date
, in that order. This is
because our queries have equality constraints on
site
and
page
, and a range query on
date
.
To create the appropriate index, we can execute the following code: