Database Reference
In-Depth Information
>>>
>>>
db
.
stats
.
daily
.
ensure_index
([
...
...
(
'metadata.site'
,
1
),
...
(
'metadata.page'
,
1
),
...
(
'metadata.date'
,
1
)])
Get data for a historical chart
To retrieve daily data for a single month, we can use the following query:
>>>
>>>
db
.
stats
.
monthly
.
find_one
(
...
...
{
'metadata'
:
...
{
'date'
:
dt
,
...
'site'
:
'site-1'
,
...
'page'
:
'/index.html'
}},
...
{
'daily'
:
1
})
To retrieve several months of daily data, we can use a variation of the preceding query:
>>>
>>>
db
.
stats
.
monthly
.
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 execute these queries efficiently, we need an index on the
monthly
aggregate similar to the
one we used for the
daily
aggregate:
>>>
>>>
db
.
stats
.
monthly
.
ensure_index
([
...
...
(
'metadata.site'
,
1
),
...
(
'metadata.page'
,
1
),
...
(
'metadata.date'
,
1
)])
This field order will efficiently support range queries for a single page over several months.
Sharding Concerns
Althoughthesystemasdesignedcansupportquitealargereadandwriteloadonasingle-mas-
ter deployment, sharding can further improve your performance and scalability. Your choice