如何找到7天内访问我网站超过3天的用户?

时间:2019-06-03 15:36:33

标签: node.js mongodb

我想找到在7天,15天和30天中都访问我的页面超过3天的用户

我发现访问我的网站的用户总数并根据该用户进行计数,但与我的搜索结果不符

我的收藏就像

{
    "_id" : ObjectId("5cf110a80556ac496396d07c"),
    "user" : "sachin",
    "event" : "page-navigation",
    "data" : {
        "url" : "/"
    },
    "addedAt" : ISODate("2019-05-14T11:31:52.755Z"),
    "__v" : 0
}

{
    "_id" : ObjectId("5cf110a80556ac496396d07c"),
    "user" : "sachin",
    "event" : "page-navigation",
    "data" : {
        "url" : "/"
    },
    "addedAt" : ISODate("2019-05-15T11:31:52.755Z"),
    "__v" : 0
}

{
    "_id" : ObjectId("5cf110a80556ac496396d07c"),
    "user" : "sachin",
    "event" : "page-navigation",
    "data" : {
        "url" : "/"
    },
    "addedAt" : ISODate("2019-05-16T11:31:52.755Z"),
    "__v" : 0
}

等...最多考虑45天

请忽略_id,根据上述示例数据,Sachin在1周内访问了我的网站3天,而在7天之内,有3天访问了我的网站

db.getCollection('usertracks').aggregate([ 
    {$project: {
            yearMonthDay: { $dateToString: { format: "%Y-%m-%d", date: "$addedAt", timezone: "+05:30" } }
    }},
    {
        $group:
        {
            _id: {
                addedAt: "$yearMonthDay",
                user: "$user"
        },
            users: { $sum: 1 }
        }
    },
])

如果在7天内有3天以上的任何时间访问我的网站,则应返回该用户的详细信息。帮我找到解决方法。

感谢您的帮助!

输出:

{
  "user":"Sachin"
}

1 个答案:

答案 0 :(得分:0)

const getUserCategoryDetails = async function(){

const activeUsers = await UserTrack.aggregate([ 
    {
        $project: {
            yearMonthDay: { $dateToString: { format:  "%Y/%m/%d", date: "$addedAt", timezone: "+05:30" } },
            user:"$user"
        }
    },
    {
        $group:
        {
            _id:"$user",
            "visitedDate": {$addToSet:{date:"$yearMonthDay"}}
        } 
    }, 
    {$unwind: "$visitedDate"},
    {$sort: {"visitedDate":1}},
    {$group: 
        {
            _id: "$_id", 
            visitDate: {$push: "$visitedDate"}
        }
    }

]);

这是根据用户查找所有日期的代码