Mongo 按开始日期和结束日期重叠的日期进行过滤

发布于 2025-01-10 02:38:21 字数 1894 浏览 1 评论 0原文

问题

给定 startQueryendQuery,什么 Mongo 过滤器会找到日期与两个查询值重叠的文档?

我所说的“重叠”是指匹配文档具有:

  1. startstartQuery 之前以及
  2. endendQuery 之后> 并且
  3. 日期位于 startQueryendQuery 之间

预期行为

startQueryendQuery2022-01 -10, 2022-01-20

文档:

[
// 01-01 is before 01-10 and 02-02 is after 01-10
{title: "match1", start: "2022-01-01T00:00:00Z", end: "2022-02-02T00:00:00Z"},

// 01-15 and 01-16 are between 01-10 and 01-15
{title: "notMatch1", start: "2022-01-15T00:00:00Z", end: "2022-01-16T00:00:00Z"},

// 01-16 is not after 01-20
{title: "notMatch2", start: "2022-01-01T00:00:00Z", end: "2022-01-16T00:00:00Z"},
]

返回:

[{title: "match1", start: "2022-01-01T00:00:00Z", end: "2022-12-31T00:00:00Z"}]

上下文

这用于查找要在日历的周视图中显示的事件。按 startOfWeekendOfWeek 进行过滤会错过跨度 3 周并与第二周重叠的事件。

我最好的猜测

这是最好的方法吗?如果是这样,我如何将其转换为 Mongo 过滤器?

queryStart = dayjs(start).dayOfYear() //10
queryEnd = dayjs(end).dayOfYear() //20 
queryArray = [10,11,12...18,19,20]    

for each document
   // this document's start and end:
   // 2022-01-01T00:00:00Z, 2022-02-02T00:00:00Z
   eventStart = dayjs(start).dayOfYear() // 1 (01-01)
   eventEnd = dayjs(end).dayOfYear()    //  33 (02-02)
   eventArray = [1,2,3, .... 31,32,33] 

   // true
   isMatch = (
               eventStart not between queryStart and queryEnd 
                &&
               eventEnd not between queryStart and queryEnd 
                &&
               any(eventArray) in (queryArray)
             ) 

Question

Given a startQuery and endQuery, what Mongo filter will find documents with dates that overlap both query values?

By 'overlap,' I mean that the matching documents have a:

  1. start before the startQuery AND
  2. end after the endQuery AND
  3. Have dates in between the startQuery and endQuery

Expected Behavior

startQuery and endQuery: 2022-01-10, 2022-01-20

Documents:

[
// 01-01 is before 01-10 and 02-02 is after 01-10
{title: "match1", start: "2022-01-01T00:00:00Z", end: "2022-02-02T00:00:00Z"},

// 01-15 and 01-16 are between 01-10 and 01-15
{title: "notMatch1", start: "2022-01-15T00:00:00Z", end: "2022-01-16T00:00:00Z"},

// 01-16 is not after 01-20
{title: "notMatch2", start: "2022-01-01T00:00:00Z", end: "2022-01-16T00:00:00Z"},
]

Return:

[{title: "match1", start: "2022-01-01T00:00:00Z", end: "2022-12-31T00:00:00Z"}]

Context

This is for finding events to display in a week view of a calendar. Filtering by startOfWeek to endOfWeek misses events that span 3 weeks and overlap the second week.

My best guess

Is this the best way? If so, how can I translate it to a Mongo filter?

queryStart = dayjs(start).dayOfYear() //10
queryEnd = dayjs(end).dayOfYear() //20 
queryArray = [10,11,12...18,19,20]    

for each document
   // this document's start and end:
   // 2022-01-01T00:00:00Z, 2022-02-02T00:00:00Z
   eventStart = dayjs(start).dayOfYear() // 1 (01-01)
   eventEnd = dayjs(end).dayOfYear()    //  33 (02-02)
   eventArray = [1,2,3, .... 31,32,33] 

   // true
   isMatch = (
               eventStart not between queryStart and queryEnd 
                &&
               eventEnd not between queryStart and queryEnd 
                &&
               any(eventArray) in (queryArray)
             ) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2025-01-17 02:38:21
db.collection.aggregate([
  {
    "$match": {
      start: {
        "$lte": "2022-01-10"
      },
      end: {
        "$gte": "2022-01-20"
      }
    }
  }
])

mongoplayground


db.collection.aggregate([
  {
    "$match": {
      $expr: {
        "$and": [
          {
            "$lte": [
              { $week: { "$toDate": "$start" } },
              { $week: { "$toDate": "2022-01-10" } }
            ]
          },
          {
            "$gte": [
              { $week: { "$toDate": "$end" } },
              { $week: { "$toDate": "2022-01-20" } }
            ]
          }
        ]
      }
    }
  }
])

mongoplayground

db.collection.aggregate([
  {
    "$match": {
      start: {
        "$lte": "2022-01-10"
      },
      end: {
        "$gte": "2022-01-20"
      }
    }
  }
])

mongoplayground


db.collection.aggregate([
  {
    "$match": {
      $expr: {
        "$and": [
          {
            "$lte": [
              { $week: { "$toDate": "$start" } },
              { $week: { "$toDate": "2022-01-10" } }
            ]
          },
          {
            "$gte": [
              { $week: { "$toDate": "$end" } },
              { $week: { "$toDate": "2022-01-20" } }
            ]
          }
        ]
      }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文