Reg:mongo db 根据查询输出错误 [英] Reg: mongo db wrong output as per query

查看:59
本文介绍了Reg:mongo db 根据查询输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下查询:

    db.logins.aggregate([
    { $match:{"loginLogoutTime":{"$gte": ISODate("2020-08-16T00:00:00.000-0400"),"$lt": ISODate("2020-08-17T23:59:59.000-0400")}}},
    { $group: {_id: {Dates: { $dateToString: { format: "%Y-%m-%d", date: "$loginLogoutTime" }},Role: "$aRole" } , uniqueCount: 
    {$addToSet: "$Uid"}}},

{ $project: { "_id" : NumberInt(0), "Dates" : "$_id.Dates", "ROLE" : "$_id.Role", "TOTALUSERS" : {$size:"$uniqueCount"} }}
])

但在输出中,我们也获得了 8 月 18 日的记录8:

but in output we are getting the records for 18th august also8 also:

output  like:
====================
{
    "Dates" : "2020-08-18",
    "ROLE" : "HSP",
    "TOTALUSERS" : 108
}

/* 50 */
{
    "Dates" : "2020-08-18",
    "ROLE" : "AFC",
    "TOTALUSERS" : 3
}

我们只需要第 16 个和第 17 个数据计数,任何建议.

We need only 16 th and 17th data count, any suggestions.

谢谢专家,我尝试了建议的修改:以下查询将提供从 8 月 16 日 00:00:00 到 8 月 17 日 23:59 的所有 Uid 唯一计数,请确认我们可以使用以下查询:

Thanks experts, I tried with suggested modification: is below query will provide the all the unique counts for Uid from 16th Aug 00:00:00 hours to 17th Aug 23:59 confirm pls can we use below query:

 db.logins.aggregate([
    { $match:{"loginLogoutTime":{"$gte": ISODate("2020-08-16T14:30:00.000-0400"),"$lt": ISODate("2020-08-17T14:32:00.000-0400")}}},
    { $group: {_id: {Dates: { $dateToString: { format: "%Y-%m-%d", date: "$loginLogoutTime" }},Role: "$aRole" } , uniqueCount: {$addToSet: "$Uid"}}},
    { $project: { "_id" : NumberInt(0), "Dates" : "$_id.Dates", "ROLE" : "$_id.Role", "TOTALUSERS" : {$size:"$uniqueCount"} }}
    ])



collection below:
========================


{
    "_id" : 55,
    "Uid" : "m955",
    "host" : "iefs-las.web.com",
    "Role" : "MGR",
    "event" : "TIMEOUT",
    "timeoutTimeStamp" : ISODate("2018-10-26T14:33:13.000Z"),
    "lastupdatedTimeStamp" : ISODate("2018-10-26T18:33:14.663Z"),
    "recordPublishIndicator" : "Y",
    "__v" : 0
}

/* 2 */
{
    "_id" : 38,
    "attUid" : "mo41b",
    "host" : "iefs-las.web.att.com",
    "agentRole" : "W_TECH",
    "event" : "LOGIN",
    "loginLogoutTime" : ISODate("2018-10-26T14:41:34.000Z"),
    "lastupdatedTimeStamp" : ISODate("2018-10-26T14:41:35.220Z"),
    "recordPublishIndicator" : "Y",
    "__v" : 0
}

推荐答案

$dateToString 默认使用UTC.您的 $match 按 -4 时区中的日期过滤记录.您观察到的结果是这 4 小时内的登录次数.

$dateToString uses UTC by default. Your $match filters records by dates in -4 timezone. The result you observe is number of logins within these 4 hrs.

根据您的需要,将时区添加到 $dateToString 或从 $match 条件中删除时区.

Either add timezone to $dateToString or remove timezone from $match conditions depending on your needs.

这篇关于Reg:mongo db 根据查询输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆