で起動するには、現在のスキーマを問い合わせるための助長されていません。配列は、あなたがキーとして平日を維持していたならば、より良いアプローチは、配列を失い、ただの要素を参照するようになり、この場合には本当に必要はありません。しかし
"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 }
})
上記の推奨事項に準拠するようにスキーマを変更する能力がない場合は、集約フレームワークが回避策を提供します。
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)
でそれを実行する前に、パイプラインを印刷:その後の$match
パイプラインで$project
ステップとクエリでdate aggregation operatorsを利用し、次の集約パイプラインを考えてみましょう:
[
{
"$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
}
}
}
]
日時のどのようにホテルの幸せな時間を保つことができますか? 幸せな時間は、私が正しい場合、日付から独立しているべきです。 – kadamb
はい私はあなたの提案に同意します。しかし、これは既存のデータベースの構造です。だから私は時間だけを比較することによってそれらをフェッチしようとしています。 – zulekha
、過去からの日付もありますか? – kadamb