【问题标题】:Group by Date mongoDB按日期分组 mongoDB
【发布时间】:2016-07-01 09:47:34
【问题描述】:

我在 mongoDB 中有一组数据,我必须将它们按$timestamp 分组。此字段包含日期,但格式为字符串(上面的示例数据)。

我应该如何继续将$timestamp 转换为日期,以便将它们组合在一起?

接下来,我必须为每个日期和标识对每个 scores_today 求和,并且对每个 scores_total 求和。

示例数据:

[
    {
        _id: "1442",
        timestamp: "2016-03-15T22:24:02.000Z",
        iden: "15",
        scores_today: "0.000000",
        scores_total: "52337.000000"
    }
]

我的代码

var project = {
            "$project":{ 
                "_id": 0,
                "y": {
                    "$year": "$timestamp"      // tried this way, not working
                    },
                "m": {
                    "$month": new Date("$timestamp") // tried either this, not working
                }, 
                "d": {
                    "$dayOfMonth":  new Date("$timestamp")
                },
                "iden" : "$iden"
            } 
        },
        group = {   
            "$group": { 
                "_id": { 
                    "iden" : "$iden",
                    "year": "$y", 
                    "month": "$m", 
                    "day": "$d"
                },  
                "count" : { "$sum" : "$scores_today" }
            }
        };
        mongoDB.collection('raw').aggregate([ project, group ]).toArray()....

这是 node.js 服务记录的错误

Err: { [MongoError: exception: can't convert from BSON type String to Date] name: 'MongoError', message: 'exception: can\'t convert from BSON type String to Date', errmsg: 'exception: can\'t convert from BSON type String to Date', code: 16006, ok: 0 }

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    您可以使用ISODate($timestamp) 从字符串构造日期对象。

    var project = {
                "$project":{ 
                    "_id": 0,
                    "y": {
                        "$year": ISODate("$timestamp").getFullYear()
                        },
                    "m": {
                        "$month": ISODate("$timestamp").getMonth()+1 // months start from 0
                    }, 
                    "d": {
                        "$dayOfMonth":  ISODate("$timestamp").getDate()
                    },
                    "iden" : "$iden"
                } 
            },
            group = {   
                "$group": { 
                    "_id": { 
                        "iden" : "$iden",
                        "year": "$y", 
                        "month": "$m", 
                        "day": "$d"
                    },  
                    "count" : { "$sum" : "$scores_today" }
                }
            };
    

    更新

    如果您没有运行 MongoDb shell,则不能直接使用 ISODate。在这种情况下尝试调用 eval 命令。

    var aggregationResult=mongoDB.eval(
    '
    'function()                                                                                '+
    '{                                                                                         '+
    '  var project = {                                                                         '+
    '              "$project":{                                                                '+ 
    '                  "_id": 0,                                                               '+
    '                  "y": {                                                                  '+
    '                      "$year": ISODate("$timestamp").getFullYear()                        '+
    '                      },                                                                  '+
    '                  "m": {                                                                  '+
    '                      "$month": ISODate("$timestamp").getMonth()+1 // months start from 0 '+
    '                  },                                                                      '+
    '                  "d": {                                                                  '+
    '                      "$dayOfMonth":  ISODate("$timestamp").getDate()                     '+
    '                  },                                                                      '+
    '                  "iden" : "$iden"                                                        '+
    '              }                                                                           '+
    '          },                                                                              '+
    '          group = {                                                                       '+
    '              "$group": {                                                                 '+
    '                  "_id": {                                                                '+
    '                      "iden" : "$iden",                                                   '+
    '                      "year": "$y",                                                       '+
    '                      "month": "$m",                                                      '+
    '                      "day": "$d"                                                         '+
    '                  },                                                                      '+
    '                  "count" : { "$sum" : "$scores_today" }                                  '+
    '              }                                                                           '+
    '          };  
    '    var result=db.raw.aggregate([ project, group ]);                  '+
    '    return result;                                                                        '+        
    '  }                                                                                       '+
    '
    );
    

    【讨论】:

    • 它给了我这个ReferenceError: ISODate is not defined
    • @a.ndrea 如果你在 mongo CLI 中输入 ISODate(); 你会看到什么结果?
    • @a.ndrea 好吧,试着把 "$timestamp" 放在引号里。
    • 我无法访问 mongo CLI,它不在我的电脑上,我也无法访问那里。连“$timestamp”都试过了,还是一样的错误,ISODate没有定义
    • 好的,您使用的是什么客户端?如果它不是 mongo CLI,那么您不能在代码中按原样使用它的函数,您必须将它们传递给 mongo,以便在聚合运行时调用它们。我想,你只能在 eval() 中传递所有聚合脚本。
    猜你喜欢
    • 2019-01-19
    • 2019-06-06
    • 2011-07-07
    • 2015-01-03
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多