简体   繁体   English

如何比较嵌套文档数组中的字段

[英]How to compare fields in an array of nested documents

I have a problem when I want to query and compare nested documents and their fields within that document. 我想查询和比较嵌套文档及其在该文档中的字段时遇到问题。 Overall I have a database with restaurants. 总的来说,我有一个餐厅数据库。 Each of the restaurants have a document called "openingHours" that contains a document for each day of the week with 3 fields 每个餐厅都有一个名为"openingHours"的文档,其中包含一周中每一天的文档,其中包含3个字段

1 The day of the week 1星期几

2 Opening time 2营业时间

3 Closing time 3关门时间

In my query the goal is to compare the opening time and closing time , for each of the documents (days). 在我的查询中,目标是比较每个文档(天)的opening timeclosing time

The problem right now is that when I run my query I get a result based on all the documents inside "openingHours" and it doesn't compare each of the documents on its own. 现在的问题是,当我运行查询时,我会基于"openingHours"所有文档获得结果,并且它不会"openingHours"比较每个文档。 Reason behind the "$expr" is that the time the restaurants close can extend to 01:00 so we need to check for that as well. "$expr"背后的原因是,餐厅关闭的时间可以延长至01:00,因此我们也需要进行检查。

Here is my query: 这是我的查询:

$or: [
      {
        $and: [
          { 
            "$expr" : {
                "$gt" : [
                    "$openingHours.open", 
                    "$openingHours.close"
                ]
            }
          },
          {
            "openingHours.close": {$gte: openingHours}
          }
        ]
      },
      {
        $and: [
          { 
            "$expr" : {
                "$gt" : [
                    "$openingHours.open", 
                    "$openingHours.close"
                ]
            }
          },
          {
            "openingHours.close": {$lte: openingHours}
          },
          {
            "openingHours.open": {$lte: openingHours}
          }
        ]
      },
      {
        $and: [
          { 
            "$expr" : {
                "$lt" : [
                    "$openingHours.open", 
                    "$openingHours.close"
                ]
            }
          },
          {
            "openingHours.open": {$lte: openingHours}
          },
          {
            "openingHours.close": {$gte: openingHours}
          }
        ]
      },
    ]
  })

Screenshot from Robo3t to help show what I mean with the documents. Robo3t的屏幕截图有助于显示我对文档的意思。 Also you can see in the image that the problem occurs when it doesn't make the query on each of the nested documents itself, since the time can differ from day to day. 您还可以在图像中看到,如果不对每个嵌套文档本身进行查询,就会出现问题,因为时间每天都有所不同。

Thanks in advance! 提前致谢!

First, !!it's a very bad idea to manage time and hours like this!! 首先, !!这样管理时间和时间是一个非常糟糕的主意! What happend if your restaurant is closed at 23:00, and i pass 2280 as param??? 如果您的餐厅在23:00关闭,并且我通过2280作为参数,会发生什么??? To deal with time without date information, it's better to base on seconds passed since 00:00:00. 要处理没有日期信息的时间,最好以从00:00:00开始经过的秒为基础。

storedTime = hours * 3600 + minutes * 60 + seconds

you have to manage your openingHours.close value with +86400 if necessary (close after midnight->+86400) before storing it. 您必须在存储之前用+86400(在午夜之后关闭-> + 86400)管理您的openHours.close值。 This way, you won't have to carry about openingHours.close greater or lesser than openingHours.open in your query. 这样,您不必在查询中携带大于或小于openingHours.open的openingHours.close。

{
"_id" : "hAZyWRwqzM5KM6TZz",
"name" : "Restaurant Spontan",
"openingHours" : [ 
    {
        "day" : 1,
        "open" : 72000,   // 20:00
        "close" : 79200   //22:00
    }, 
    {
        "day" : 2,
        "open" : 54000,    //   15:00
        "close" : 90000     // <= 01:00 = 24:00 (86400) + 01:00 (3600)
    }, 

   ...
]
}

Now you can easily query openingHours with element matching both day and current hour/minute between open and close times. 现在,您可以轻松地查询openHours,其元素在打开和关闭时间之间都匹配日期和当前小时/分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM