1

how to get the most visited pages / entries per day / week / month with mongodb

This solution is good for stats, but no for list of most visited entries MongoDB Approaches for storing large amounts of metrics / analytics data

map/reduce is too slow... or not?

thanks

Community
  • 1
  • 1
AHOYAHOY
  • 1,856
  • 4
  • 24
  • 34

1 Answers1

2

Based on the example that you have referred, there is a collection created where the url hits are present in hourly basis.

Assuming the sample doc like this

{page: "/index.html", time: Date( "Mon Apr 18 07:49:28 2011"), views: 53, tweets: 2}

You can use the Mongodb's aggregation framework. Following is the sudo code for aggregating the views on a monthly basis.

    db.pagestats.aggregate( { $project : { month_hits : { $month : "$time" } } },    
    { $group:{_id : {$month_hits:"$month_hits"},hits: { $sum: "$views" } } }, 
    { $sort : { "_id.hits" : 1 }} } );

Refer to this link for more details on aggregation.

Map-Reduce is not meant for real-time queries. So you can do similar aggregation and save the result in temp collection and use it for real time querying. You can do incremental MapReduce, so that you need not run MapReduce for the whole collection again. Read here for more details.

Incremental MapReduce would be my choice for this scenario.

Sundar
  • 387
  • 3
  • 12