如何在mongoDB中过滤从hh:mm到hh:mm两次之间的数据
How to filter data between two times from hh:mm to hh:mm in mongoDB
猫鼬
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)。
您可以使用 $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
查询:
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);
}
});
一种更有效的方法将涉及使用 $redact
运算符的单个管道,如下所示:
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);
}
});
猫鼬
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)。
您可以使用 $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
查询:
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);
}
});
一种更有效的方法将涉及使用 $redact
运算符的单个管道,如下所示:
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);
}
});