Mongo 按开始日期和结束日期重叠的日期进行过滤
问题
给定 startQuery
和 endQuery
,什么 Mongo 过滤器会找到日期与两个查询值重叠的文档?
我所说的“重叠”是指匹配文档具有:
start
在startQuery
之前以及end
在endQuery
之后> 并且- 日期位于
startQuery
和endQuery
之间
预期行为
startQuery
和 endQuery
:2022-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"}]
上下文
这用于查找要在日历的周视图中显示的事件。按 startOfWeek
到 endOfWeek
进行过滤会错过跨度 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:
start
before thestartQuery
ANDend
after theendQuery
AND- Have dates in between the
startQuery
andendQuery
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mongoplayground
mongoplayground
mongoplayground
mongoplayground