Database Reference
In-Depth Information
more granular. In this example, a customer_id will be added to the
_id to allow for aggregation of metrics for each customer.
The aggregation itself is accomplished through update commands:
> db.metrics.update({_id:"1:201312010000"},
{$set:{customer_id:1,
ts:new Date(2013,12-1,01,00,00)},
$inc:{"metrics.visitors":1}},{upsert:true});
> db.metrics.update({_id:"1:201312010000"},
{$set:{customer_id:1,
ts:new Date(2013,12-1,01,00,00)},
$inc:{"metrics.clicks":1}},{upsert:true});
> db.metrics.update({_id:"1:201312010000"},
{$set:{customer_id:1,
ts:new Date(2013,12-1,01,00,00)},
$inc:{"metrics.views":1}},{upsert:true});
> db.metrics.update({_id:"1:201312010000"},
{$set:{customer_id:1,
ts:new Date(2013,12-1,01,00,00)},
$inc:{"metrics.visitors":1}},{upsert:true});
> db.metrics.find();
{ "_id" : "1:201312010000", "customer_id" : 1,
"metrics" : {
"clicks" : 1,
"views" : 1,
"visitors" : 2
},
"ts" : ISODate("2013-12-01T08:00:00Z")
}
Notice that the upsert option is set to true . This causes the document
to be created if one that matches it does not exist. This is why the
customer_id and ts fields are set on each call, to ensure they are set
to the appropriate value if the document must be created.
 
Search WWH ::




Custom Search