我有一个MongoDB用户集合.用户可以随时激活和停用自己.我正在存储每次激活的开始和结束日期.现在,我想要获取在特定日期处于活动状态的用户列表.
记录是这样的:
{
"active" : true,
"endDates" : [
16.11.2021,
27.06.2020
],
"startDates" : [
21.10.2022,
16.10.2021,
09.04.2020
]
}
在上面的示例中,startDates多于endDates,因为用户此时处于活动状态.
现在,如果我想判断用户在12.05.2020或22.11.2022上是否处于活动状态,它应该返回TRUE,但它应该返回17.12.2021的FALSE.
我try 在endDates和startDates上使用展开,如下所示:
collection
.aggregate([
{ $match: {} },
{ $unwind: '$endDates' },
{ $unwind: '$startDates' },
])
但它给了我所有可能的开始和结束日期组合,并返回6个文档,这对查找日期范围没有用处.