【问题标题】:mongoDB: mongoose vs command line aggregationmongoDB:猫鼬与命令行聚合
【发布时间】:2017-06-25 21:54:53
【问题描述】:

我正在尝试使用以下内容运行 mongoDB 聚合:

Procedures.aggregate(
        {
            $match : {
                funded: true,
                updatedate : { $gte : new Date(startOfMonth) }
            }
        },

        {

            $project: {
                _id : 0,
                amount: 1,
                date : '$updatedate', 
                h: {$hour : '$updatedate'}, 
                m: {$minute : '$updatedate'}, 
                s: {$second : '$updatedate'}, 
                ml: {$millisecond : '$updatedate'}

            },

            $project: {
                _id: 0,
                amount: 1,
                date : {
                    $subtract : [
                        '$date',
                        { 
                            $add: [
                                '$ml',
                                {$multiply : ['$s',1000]},
                                {$multiply : ['$m',60,1000]},
                                {$multiply : ['$h',60,60,1000]}
                            ]
                        }
                    ]
                } // transform to simple date 01/01/2016
            }
        },

        {
            $group: {
                _id: {
                    d: '$date'
                },  
                total : {  
                    $sum : "$amount"  
                },  
                count: { $sum: 1 }
            }
        },

        function(err, data){

            if (err) {
                console.log("ERROR: DashboardModel::getDailyDataForMonth: " + err);
            }
            console.log(JSON.stringify(data));
            return callback();
    });

问题是当我使用命令行运行它时,相同的聚合工作正常:

db.procedures.aggregate([{$match : {funded: true,updatedate : { $gte : new Date('2016-01-01T08:00:00.000Z'), $lt: new Date('2016-01-31T08:00:00.000Z') }}}, { $project: {_id: 0, date: '$updatedate', amount: 1, h : {$hour : '$updatedate'},m : {$minute : '$updatedate'},s : {$second : '$updatedate'},ml : {$millisecond : '$updatedate'}} }, {$project: {_id: 0, amount: 1, date : {$subtract : ['$date',{ $add : ['$ml',{$multiply : ['$s',1000]},{$multiply : ['$m',60,1000]},{$multiply : ['$h',60,60,1000]}]}]}} }, { $group: {_id: {d: '$date' }, total: {$sum: "$amount"}, count: {$sum: 1}} }]

{ "_id" : { "d" : ISODate("2016-01-07T00:00:00Z") }, "total" : 389, "count" : 3 }

猫鼬返回的地方

[{"_id":{"d":null},"total":389,"count":3}]

我不确定自己做错了什么。

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    如果您只是想将日期转换为特定格式,请在 $group 管道中使用 $dateToString 运算符:

    Procedures.aggregate(
        {
            "$match": {
                "funded": true,
                "updatedate": { "$gte" : new Date(startOfMonth) }
            }
        },
        {
            "$group": {
                "_id": {
                    "yearMonthDay": { 
                        "$dateToString": { 
                            "format": "%Y-%m-%d", 
                            "date": "$updatedate" 
                        } 
                    }
                },
                "total": { "$sum": "$amount" },
                "count": { "$sum": 1 }
            }
        },
    
        function(err, data){
    
            if (err) {
                console.log("ERROR: DashboardModel::getDailyDataForMonth: " + err);
            }
            console.log(JSON.stringify(data));
            return callback();
    });
    

    【讨论】:

    • Chridam 感谢您的回复。它运行良好。
    猜你喜欢
    • 2019-08-29
    • 2013-06-24
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2014-12-10
    相关资源
    最近更新 更多