【发布时间】:2019-09-02 18:43:46
【问题描述】:
如果 start_date 是 '2019-08-10' 而 end_date 是 '2019-09-15' 那么我希望 o/p 为
'2019-08-10'
'2019-08-11'
'2019-08-12'
'2019-08-13'
'2019-08-14'
............
............
............
'2019-09-14'
'2019-09-15'
我尝试使用 $range 函数,但是当日期差异很大(超过 100 天)时,它会抛出错误 "$range requires an ending value that can be represented as a 32-bit integer, found value: 1.57248e+10"
db.test.aggregate([
{ $addFields: { days_in_millis: { $add: [{ $subtract: ["$end_date", "$start_date"] }, 86400000] } } },
{ $project: { end_date: 1, start_date: 1, millis_range: { $range: [0, "$days_in_millis", 86400000] } } },
{
$project: {
dates_in_between_inclusive: {
$map: {
input: "$millis_range",
as: "millis_count",
in: { $add: ["$start_date", "$$millis_count"] }
}
}
}
},
{ $unwind: "$dates_in_between_inclusive" }
])
【问题讨论】:
-
这些开始和结束日期是字符串还是真实日期?实际上,必须是真实的日期,否则
$subtract将不起作用...
标签: mongodb mongodb-query aggregation-framework