Mongo 按开始日期和结束日期重叠的日期过滤
Mongo filter by dates that overlap both start and end dates
问题
给定 startQuery
和 endQuery
,什么 Mongo 过滤器会找到日期与两个查询值重叠的文档?
'overlap,' 我的意思是匹配文档有一个:
start
在 startQuery
AND 之前
end
在 endQuery
AND 之后
- 日期介于
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"},
]
Return:
[{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)
)
db.collection.aggregate([
{
"$match": {
start: {
"$lte": "2022-01-10"
},
end: {
"$gte": "2022-01-20"
}
}
}
])
db.collection.aggregate([
{
"$match": {
$expr: {
"$and": [
{
"$lte": [
{ $week: { "$toDate": "$start" } },
{ $week: { "$toDate": "2022-01-10" } }
]
},
{
"$gte": [
{ $week: { "$toDate": "$end" } },
{ $week: { "$toDate": "2022-01-20" } }
]
}
]
}
}
}
])
问题
给定 startQuery
和 endQuery
,什么 Mongo 过滤器会找到日期与两个查询值重叠的文档?
'overlap,' 我的意思是匹配文档有一个:
start
在startQuery
AND 之前
end
在endQuery
AND 之后
- 日期介于
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"},
]
Return:
[{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)
)
db.collection.aggregate([
{
"$match": {
start: {
"$lte": "2022-01-10"
},
end: {
"$gte": "2022-01-20"
}
}
}
])
db.collection.aggregate([
{
"$match": {
$expr: {
"$and": [
{
"$lte": [
{ $week: { "$toDate": "$start" } },
{ $week: { "$toDate": "2022-01-10" } }
]
},
{
"$gte": [
{ $week: { "$toDate": "$end" } },
{ $week: { "$toDate": "2022-01-20" } }
]
}
]
}
}
}
])