如何将以下“点击”集合合并到“会话”集合中:
点击次数:
{session_id: 2, time: 12},
{session_id: 2, time: 12.1},
{session_id: 3, time: 13},
会议:
{session_id:1, start_time: 1, clicks: 1},
{session_id:2, start_time: 2, clicks: 1}
因此集合“会话”变为:
{session_id: 1, start_time: 1, clicks: 1}, // "old" sessions data remains as is
{session_id: 2, start_time: 2, clicks: 3}, // start_time is still 2, clicks have incremented
{session_id: 3, start_time: 13, clicks: 1} // a session with a new session_id is added
型
并且使得当更多的点击被添加到“点击”集合并在{time: { $gt: 13 }}
上对其进行过滤时可以重复该过程(以避免再次处理相同的点击)。
1条答案
按热度按时间rdrgkggo1#
假设session_id是一个唯一字段(由唯一索引强制执行),您可以先使用
$group
将clicks
记录按session_id
分组,然后将$merge
分组到sessions
集合中。Mongo Playground