MongoDB存储大量度量/分析数据的方法 [英] MongoDB Approaches for storing large amounts of metrics / analytics data
问题描述
我们正在计划使用MongoDB存储大量分析数据,例如视图和点击次数。我不确定在MongoDB中构建文档的最佳方法是帮助查询和减少数据库大小。
We are planning on using MongoDB to store large amounts of analytics data such as views and clicks. I'm unsure on the best way to structure the documents within MongoDB to aid querying and reduce database size.
我们需要再次记录一个pagename,客户端和操作类型。理想情况下,我们需要按年/月/日/小时级别的统计数据,我们不需要或关心每秒或分钟的观看次数。虽然这个文档结构看起来不错,我知道100个vistor会生成100个新的文档。
We need to record actions agains a pagename, client and the type of action. Ideally we need stats which go down the the year/month/day/hour level, we don't need or care about views per second or minute. While this document structure looks ok, I'm aware 100 vistors would generate a 100 new documents.
{_id:ObjectId(4dabdef81a34961506040000 ),
pagename:Hello,
action:view,
client:client-name,
time (Mon Apr 18 07:49:28 2011)}
最好的做法是使用 $ inc 或
Is there best practice way of doing this, either using $inc or Capped Collections?
推荐答案
更新答案 / strong>
Updated answer
在mongo shell中一起入侵:
Hacked together in the mongo shell:
use pagestats;
// a little helper function
var pagePerHour = function(pagename) {
d = new Date();
return {
page : pagename,
year: d.getUTCFullYear(),
month: d.getUTCMonth(),
day : d.getUTCDate(),
hour: d.getUTCHours(),
}
}
// a pageview happened
db.pagestats.update(
pagePerHour('Hello'),
{ $inc : { views : 1 }},
true ); //we want to upsert
// somebody tweeted our page twice!
db.pagestats.update(
pagePerHour('Hello'),
{ $inc : { tweets : 2 }},
true ); //we want to upsert
db.pagestats.find();
// { "_id" : ObjectId("4dafe88a02662f38b4a20193"),
// "year" : 2011, "day" : 21, "hour" : 8, "month" : 3,
// "page" : "Hello",
// "tweets" : 2, "views" : 1 }
// 24 hour summary 'Hello' on 2011-4-21
for(i = 0; i < 24; i++) {
//careful: days (1-31), month (0-11) and hours (0-23)
stats = db.pagestats.findOne({ page: 'Hello', year: 2011, month: 3, day : 21, hour : i})
if(stats) {
print(i + ': ' + stats.views + ' views')
} else {
print(i + ': no hits')
};
}
根据您要跟踪的方面,您可以考虑添加更多集合(例如用于以用户为中心的跟踪的集合)。希望有帮助。
Depending on which aspects you want to track you might consider adding more collections (e.g. a collection for user centric tracking). Hope that helps.
另请参阅
关于Google Analytics(分析)数据的Blogpost
这篇关于MongoDB存储大量度量/分析数据的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!