我有一个事件集合,user_id
和created_at
(ISO日期)。
我需要每天对对象的数量求和,并且每天只计算一次user_id
的事件。
我得到了下一个查询,但它计算了每天同一用户的所有事件:
results = Event.collection.aggregate([
{"$match" => {
"created_at" => {
"$gte" => start_time, "$lte" => end_time
},
"event_type" => "click"
}},
{"$group" => {
"_id" => {
"$concat" => [
{"$substr" => [{"$year" => "$created_at"}, 0, 4]},
'-',
{"$cond" => [
{"$lt" => [{"$month" => "$created_at"}, 9]},
{"$concat" => ["0", {"$substr" => [{"$month" => "$created_at"}, 0, 2]}]},
{"$substr" => [{"$month" => "$created_at"}, 0, 2]}
]},
'-',
{"$cond" => [
{"$lt" => [{"$dayOfMonth" => "$created_at"}, 9]},
{"$concat" => ["0", {"$substr" => [{"$dayOfMonth" => "$created_at"}, 0, 2]}]},
{"$substr" => [{"$dayOfMonth" => "$created_at"}, 0, 2]}
]}
]
},
"count" => {"$sum" => 1}
}}
])
我试着将它与答案结合起来: Mongoid Grouping by date
但它对我不起作用。
如何区分user_id
AKA会忽略同一天同一天的多条记录?
由于
答案 0 :(得分:1)
我得到了这个,我只是在代码后面计算users_unique数组长度。
db.events.aggregate(
{$match: {event_type: 'click', created_at: {
$gte: start_time,
$lt: end_time
}}},
{$project: {
user_id: $user_id,
year: {$year: $created_at},
month: {$month: $created_at},
day: {$dayOfMonth: $created_at}
}},
{$group: {
_id: {year: $year, month: $month, day: $day},
unique_users: {$addToSet: $user_id},
total_sessions: {$sum: 1}
}}
)