gpt4 book ai didi

mongodb - 我们可以只比较MongoDB中的时间吗?

转载 作者:IT老高 更新时间:2023-10-28 12:32:09 24 4
gpt4 key购买 nike

我已经以日期时间格式存储了happyHours 酒店。现在我想获取所有happyHours 大于当前时间的酒店。

据我所知,我知道有日期比较来获取结果,但这会比较整个日期时间对象。

有没有办法只比较时间?

db 中的欢乐时光日期对象示例:

"happyHours" : {
"mon" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
],
"tue" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
],
"wed" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
],
"thu" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
],
"fri" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
],
"sat" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
],
"sun" : [
{
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
]
}

最佳答案

首先,当前模式不利于查询;在这种情况下,数组实际上是不必要的,如果您要将工作日作为键,那么更好的方法是丢失数组并仅引用元素:

"happyHours": {
"mon": {
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
},
...
"sun": {
"startTime" : ISODate("2016-04-11T06:30:59.000Z"),
"endTime" : ISODate("2016-04-11T14:30:59.000Z")
}
}

但是,一个更好的设计使查询更容易招手。您可以将 happyHours 字段转换为一个数组,该数组包含一个文档,该文档分别描述了工作日、开始时间、开始分钟、结束小时和分钟,如下所示:

"happyHours": [
{
"weekDay": "mon",
"startHour": 6,
"startMinutes": 30,
"endHour": 14
"endMinutes": 30
}
...
]

然后查询如下:

var now = new Date(),
days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sun'],
weekDay = days[now.getDay()],
hour = now.getHours(),
minutes = now.getMinutes();

db.hotels.find({
"happyHours.weekDay": weekDay,
"happyHours.startHour": { "$lte": hour },
"happyHours.startMinutes": { "$lte": minutes },
"happyHours.endHour": { "$gte": hour },
"happyHours.endMinutes": { "$gte": minutes }
})

现在,如果您没有能力修改架构以符合上述建议,那么聚合框架提供了一种解决方法。考虑以下使用 date aggregation operators 的聚合管道。在 $project 步骤和后续 $match 中的查询 管道:

 var now = new Date(),
days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sun'],
weekDay = days[now.getDay()],
hour = now.getHours(),
minutes = now.getMinutes();
project = {
"$project": {
"otherfields": 1,
"happyHours": 1,
"happyHoursMonStartHour": { "$hour": "$happyHours.mon.startTime" },
"happyHoursMonStartMinutes": { "$minute": "$happyHours.mon.startTime" },
"happyHoursMonEndHour": { "$hour": "$happyHours.mon.endTime" },
"happyHoursMonEndMinutes": { "$minute": "$happyHours.mon.endTime" },
"happyHoursTueStartHour": { "$hour": "$happyHours.tue.startTime" },
"happyHoursTueStartMinutes": { "$minute": "$happyHours.tue.startTime" },
"happyHoursTueEndHour": { "$hour": "$happyHours.tue.endTime" },
"happyHoursTueEndMinutes": { "$minute": "$happyHours.tue.endTime" },
"happyHoursWedStartHour": { "$hour": "$happyHours.wed.startTime" },
"happyHoursWedStartMinutes": { "$minute": "$happyHours.wed.startTime" },
"happyHoursWedEndHour": { "$hour": "$happyHours.wed.endTime" },
"happyHoursWedEndMinutes": { "$minute": "$happyHours.wed.endTime" },
"happyHoursThuStartHour": { "$hour": "$happyHours.thu.startTime" },
"happyHoursThuStartMinutes": { "$minute": "$happyHours.thur.startTime" },
"happyHoursThuEndHour": { "$hour": "$happyHours.thu.endTime" },
"happyHoursThuEndMinutes": { "$minute": "$happyHours.thu.endTime" },
"happyHoursFriStartHour": { "$hour": "$happyHours.fri.startTime" },
"happyHoursFriStartMinutes": { "$minute": "$happyHours.fri.startTime" },
"happyHoursFriEndHour": { "$hour": "$happyHours.fri.endTime" },
"happyHoursFriEndMinutes": { "$minute": "$happyHours.fri.endTime" },
"happyHoursSatStartHour": { "$hour": "$happyHours.sat.startTime" },
"happyHoursSatStartMinutes": { "$minute": "$happyHours.sat.startTime" },
"happyHoursSatEndHour": { "$hour": "$happyHours.sat.endTime" },
"happyHoursSatEndMinutes": { "$minute": "$happyHours.sat.endTime" },
"happyHoursSunStartHour": { "$hour": "$happyHours.sun.startTime" },
"happyHoursSunStartMinutes": { "$minute": "$happyHours.sun.startTime" },
"happyHoursSunEndHour": { "$hour": "$happyHours.sun.endTime" },
"happyHoursSunEndMinutes": { "$minute": "$happyHours.sun.endTime" },
}
},
match = { "$match": {} },
pipeline = [
{ "$unwind": "$happyHours.mon" },
{ "$unwind": "$happyHours.tue" },
{ "$unwind": "$happyHours.wed" },
{ "$unwind": "$happyHours.thur" },
{ "$unwind": "$happyHours.fri" },
{ "$unwind": "$happyHours.sat" },
{ "$unwind": "$happyHours.sun" }
];

match["$match"]["happyHours"+ weekDay +"StartHour"] = { "$lte": hour };
match["$match"]["happyHours"+ weekDay +"StartMinutes"] = { "$lte": minutes };
match["$match"]["happyHours"+ weekDay +"EndHour"] = { "$gte": minutes };
match["$match"]["happyHours"+ weekDay +"EndMinutes"] = { "$gte": minutes };
pipeline.push(project);
pipeline.push(match);

db.hotels.aggregate(pipeline);

在使用 printjson(pipeline) 运行管道之前打印管道会显示:

[
{
"$unwind" : "$happyHours.mon"
},
{
"$unwind" : "$happyHours.tue"
},
{
"$unwind" : "$happyHours.wed"
},
{
"$unwind" : "$happyHours.thur"
},
{
"$unwind" : "$happyHours.fri"
},
{
"$unwind" : "$happyHours.sat"
},
{
"$unwind" : "$happyHours.sun"
},
{
"$project" : {
"otherfields" : 1,
"happyHours" : 1,
"happyHoursMonStartHour" : {
"$hour" : "$happyHours.mon.startTime"
},
"happyHoursMonStartMinutes" : {
"$minute" : "$happyHours.mon.startTime"
},
"happyHoursMonEndHour" : {
"$hour" : "$happyHours.mon.endTime"
},
"happyHoursMonEndMinutes" : {
"$minute" : "$happyHours.mon.endTime"
},
"happyHoursTueStartHour" : {
"$hour" : "$happyHours.tue.startTime"
},
"happyHoursTueStartMinutes" : {
"$minute" : "$happyHours.tue.startTime"
},
"happyHoursTueEndHour" : {
"$hour" : "$happyHours.tue.endTime"
},
"happyHoursTueEndMinutes" : {
"$minute" : "$happyHours.tue.endTime"
},
"happyHoursWedStartHour" : {
"$hour" : "$happyHours.wed.startTime"
},
"happyHoursWedStartMinutes" : {
"$minute" : "$happyHours.wed.startTime"
},
"happyHoursWedEndHour" : {
"$hour" : "$happyHours.wed.endTime"
},
"happyHoursWedEndMinutes" : {
"$minute" : "$happyHours.wed.endTime"
},
"happyHoursThuStartHour" : {
"$hour" : "$happyHours.thu.startTime"
},
"happyHoursThuStartMinutes" : {
"$minute" : "$happyHours.thur.startTime"
},
"happyHoursThuEndHour" : {
"$hour" : "$happyHours.thu.endTime"
},
"happyHoursThuEndMinutes" : {
"$minute" : "$happyHours.thu.endTime"
},
"happyHoursFriStartHour" : {
"$hour" : "$happyHours.fri.startTime"
},
"happyHoursFriStartMinutes" : {
"$minute" : "$happyHours.fri.startTime"
},
"happyHoursFriEndHour" : {
"$hour" : "$happyHours.fri.endTime"
},
"happyHoursFriEndMinutes" : {
"$minute" : "$happyHours.fri.endTime"
},
"happyHoursSatStartHour" : {
"$hour" : "$happyHours.sat.startTime"
},
"happyHoursSatStartMinutes" : {
"$minute" : "$happyHours.sat.startTime"
},
"happyHoursSatEndHour" : {
"$hour" : "$happyHours.sat.endTime"
},
"happyHoursSatEndMinutes" : {
"$minute" : "$happyHours.sat.endTime"
},
"happyHoursSunStartHour" : {
"$hour" : "$happyHours.sun.startTime"
},
"happyHoursSunStartMinutes" : {
"$minute" : "$happyHours.sun.startTime"
},
"happyHoursSunEndHour" : {
"$hour" : "$happyHours.sun.endTime"
},
"happyHoursSunEndMinutes" : {
"$minute" : "$happyHours.sun.endTime"
}
}
},
{
"$match" : {
"happyHoursThuStartHour" : {
"$lte" : 9
},
"happyHoursThuStartMinutes" : {
"$lte" : 34
},
"happyHoursThuEndHour" : {
"$gte" : 34
},
"happyHoursThuEndMinutes" : {
"$gte" : 34
}
}
}
]

关于mongodb - 我们可以只比较MongoDB中的时间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36977539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com