用户登陆数据存放在MongoDB中,每次登陆操作,都会插入一条记录,现在统计出,每天(指定时区)有多少个不同的用户登录成功
{ "_id" : ObjectId("5da859dc062b4408b34555c8"), "userId" : "79e1a83fc7e3407aaaf6dc7862461e4f", "account" : "j111@163", "loginResult" : "success", "failReason" : "", "ip" : "192.168.0.186", "loginTime" : ISODate("2019-10-17T12:09:00.904+0000"),
}
// 查询条件Criteria criteria = Criteria.where("loginResult").is(Constants.SUCCESS_STR);// 格式化日期 YY-MM-DD,并且指定时区,Mongo不区分时区AggregationExpression dateExpression = DateOperators.DateToString.dateOf("loginTime").toString("%Y-%m-%d").withTimezone(DateOperators.Timezone.valueOf("+08"));Aggregation aggregation = wAggregation(Aggregation.match(criteria),Aggregation.project("account").and(dateExpression).as("date"),// 按日期和用户分组,去重用户,一个用户,一天只算一次up("date", "account").first("date").as("date").first("account").as("account"),// 统计按日期分组,统计每天登录的成功的总人数up("date").count().as("loginUserNumber").first("date").as("date"));List<JSONObject> list = mongoTemplate.aggregate(aggregation, "USER_LOGIN_RECORD", JSONObject.class).getMappedResults();System.out.JSONString(list));# 结果[{"date":"2019-10-17","loginUserNumber":9,"_id":"2019-10-17"},{"date":"2019-10-18","loginUserNumber":8,"_id":"2019-10-18"}]
本文发布于:2024-01-31 18:15:27,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170669612930429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |