I'm using mongodb to store image metadata and S3 urls. I would like to show most viewed items on a time period, like 'today', 'this week' , just like youtube does with videos. How should I store 'views' on mongodb to query that kind of data?
I'm thinking of creating a view collection and storing each 'view' as a document with date information, and also storing the view count in individual image documents for fast retrieval of view counts. These two will be updated on each pageview. I'm assuming MapReduce would get开发者_开发知识库 the job done form there, but I can't quite picture it.
Do not create a separate document for each view, unless you need it.
You could chop the not significant part of the date, store only the day+hour or only the day, and use $inc
to add a new 'view':
db.hits.update(
{"item": "image0001", "date": ..dayordayandhour..},
{"$inc": { "hits": 1}},
true) //upsert
For the aggregation you can use group
, mapreduce is an overkill for this.
Note: If you do not need exact numbers you can do the aggregation on a hourly basis and then update the "this day"/"this week" counters (you can manintain multiple counters with this approach!) on each document so you can do quick lookups and sorting based on view counters.
精彩评论