如何在mongodb聚合中获取两个日期之间的工作日数

如何在mongodb聚合中获取两个日期之间的工作日数,mongodb,mongodb-query,aggregation-framework,aggregate-functions,Mongodb,Mongodb Query,Aggregation Framework,Aggregate Functions,我有两个日期开始日期:2019-08-13和结束日期:2019-08-20。我希望结束日期与开始日期之间的差异以工作日(不包括节假日)为单位。因此,我的结果将是4,因为2019-08-15是节假日,而2019-08-17,2019-08-18是周末。尝试以下查询: db.collection.aggregate([ {$addFields: {days_in_millis: { $add: [{$subtract: ["$end_date", "$start_date"]}, 86400000

我有两个日期
开始日期:2019-08-13
结束日期:2019-08-20
。我希望结束日期与开始日期之间的差异以工作日(不包括节假日)为单位。因此,我的结果将是
4
,因为
2019-08-15
是节假日,而
2019-08-17
2019-08-18
是周末。

尝试以下查询:

db.collection.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"},
{$project: {date: "$dates_in_between_inclusive", is_not_weekend: {
    $cond: {
    if: { $in: [ {$dayOfWeek: "$dates_in_between_inclusive"}, [1, 7] ]},
    then: 0,
    else: 1
}}}},
{$match: {date: {$nin: holidays_dates_list}}},
{$group: {
    _id: "$_id",
    days: {$sum: "$is_not_weekend"}
}}
])
假设:

1. every document has at least start_date and end_date fields which are mongodb dates.
2. "holidays_dates_list" is an array of dates which has holidays (may or may not include weekends)
上面的查询本身过滤周末。因此,“假日日期列表”不需要有周末。

尝试以下查询:

db.collection.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"},
{$project: {date: "$dates_in_between_inclusive", is_not_weekend: {
    $cond: {
    if: { $in: [ {$dayOfWeek: "$dates_in_between_inclusive"}, [1, 7] ]},
    then: 0,
    else: 1
}}}},
{$match: {date: {$nin: holidays_dates_list}}},
{$group: {
    _id: "$_id",
    days: {$sum: "$is_not_weekend"}
}}
])
假设:

1. every document has at least start_date and end_date fields which are mongodb dates.
2. "holidays_dates_list" is an array of dates which has holidays (may or may not include weekends)

上面的查询本身过滤周末。因此,“假日\日期\列表”不需要周末。

我们如何获取假日信息?@S.Sharma先生我们有一个存储假日的集合。该集合中很少有对象看起来像{date:'2019-08-14',day:周三',holiday:false}{date:'2019-08-15',day:'Thurseday',holiday:true}如果这是假日的集合,那么为什么
假日:false
?@Mr s.Sharma在该集合中所有的日期都存储有假日标志。但是,即使我们只存储假日,如何实现上述场景。我们如何获取有关假日的信息?@Mr.S.Sharma我们有一个存储假日的集合。该集合中很少有对象看起来像{date:'2019-08-14',day:周三',holiday:false}{date:'2019-08-15',day:'Thurseday',holiday:true}如果这是假日的集合,那么为什么
假日:false
?@Mr s.Sharma在该集合中所有的日期都存储有假日标志。但是,即使我们只存储假日,如何实现上述场景。当假日日历位于另一个集合中,并且我收到错误{“ok”:0,“errmsg”:“$range需要一个可以表示为32位整数的结束值,找到的值:1.57248e+10”,“code”:34446,“codeName”:“Location34446”}:差异更大时聚合失败。当假日日历位于另一个集合中并且我收到错误{“ok”:0,“errmsg”:“$range需要一个可以表示为32位整数的结束值,找到的值:1.57248e+10”,“code”:34446,“codeName”:“Location34446”}:差异大于时聚合失败。