如何在mongoDB中从hh:mm到hh:mm的两次过滤数据 [英] How to filter data between two times from hh:mm to hh:mm in mongoDB

查看:173
本文介绍了如何在mongoDB中从hh:mm到hh:mm的两次过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

猫鼬

var filter = {};
filter.strBillDate = { 
    "$gte": new Date(req.params.fromdate), 
    "$lt": new Date(req.params.todate)    
};
return Sales
    .aggregate([{
        $match: filter
    }, {
        "$project": {
            "strBillNumber": 1,
            "strBillAmt": 1,
            "store_id": 1,
            "strBillDate": 1,
            "hourPart": {
                "$hour": "$strBillDate"
            },
            "minutePart": {
                "$minute": "$strBillDate"
            },
        }
    }, {
        "$match": 
            { "hourPart": { "$gte": fromhour, "$lte": tohour } }
    }])
    .exec(function(err, salesdata) {
        if (!err) {
            return res.send(salesdata);
        }
    });

这里我可以过滤两个小时之间的数据(例如:17到19).但是我需要从hh:mm&到hh:mm(例如17:15至19:30).

Here I can filter data between two hours(Ex: 17 to 19). But I need to filter that data from hh:mm && to hh:mm(Eg: 17:15 to 19:30).

推荐答案

您可以使用 $match 查询:

You could use the $dateToString operator to project a time string field with the format HH:MM that you can then do a direct string comparison in the $match query:

var filter = {};
filter.strBillDate = { 
    "$gte": new Date(req.params.fromdate), 
    "$lt": new Date(req.params.todate)    
};
return Sales
    .aggregate([{
        $match: filter
    }, {
        "$project": {
            "strBillNumber": 1,
            "strBillAmt": 1,
            "store_id": 1,
            "strBillDate": 1,
            "time": { "$dateToString": { "format": "%H:%M", date: "$strBillDate" } }
        }
    }, {
        "$match": 
            { "time": { "$gte": "17:15", "$lte": "19:30" } }
    }])
    .exec(function(err, salesdata) {
        if (!err) {
            return res.send(salesdata);
        }
    });


更有效的方法将涉及使用


A more efficient approach would involve a single pipeline that uses the $redact operator as follows:

Sales.aggregate([
    { 
        "$redact": { 
            "$cond": [
                { 
                    "$and": [  
                        { "$gte": [ "$strBillDate", new Date(req.params.fromdate) ] },
                        { "$lt": [ "$strBillDate", new Date(req.params.todate) ] },
                        { 
                            "$gte": [ 
                                { 
                                    "$dateToString": { 
                                        "format": "%H:%M", 
                                        "date": "$strBillDate" 
                                    } 
                                }, 
                                "17:15"
                            ] 
                        },
                        { 
                            "$lte": [ 
                                { 
                                    "$dateToString": { 
                                        "format": "%H:%M", 
                                        "date": "$strBillDate" 
                                    } 
                                }, 
                                "19:30" 
                            ] 
                        }
                    ]
                },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    }
]).exec(function(err, salesdata) {
    if (!err) {
        return res.send(salesdata);
    }
});

这篇关于如何在mongoDB中从hh:mm到hh:mm的两次过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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