我想计算某个特定用户查看某条记录的次数。它的查询是什么?
我有一个记录用户活动的集合。每次用户查看某个内容时,都会在该集合中创建一行。我想计算用户做某事的次数
我正在尝试这个,但不工作
db.activities.aggregate(
{$group: {
_id: "$assetName",
count: { $sum: 1}
}},
{$match: {
"userId": "1"
}}
);
User A viewed Company ABC
User A viewed Company XYZ
再次User A viewed Company ABC
User B viewed company TEST
预期结果
1. Company A viewed 2 times by user 1
2. Company B viewed 5 times by user 1
试试这个
db.activities.aggregate({$group: {_id: {comp: "$assetName",
rec_id: "$recordId",
user: "$userId"},
count: {$sum: 1}}})