mongoDB查找多个字段中的任何一个与日期部分范围匹配的记录

时间:2019-11-13 20:03:21

标签: javascript mongodb

在这种情况下,我有一个搜索界面,该界面使用户可以按日期范围或日期的任何部分在系统中查找条目。我的挑战是日期范围可以应用于多个日期中的任何一个。

这是一个用户数据模型(当然是简化的)

{
  name: string
  birth_date: date
  hire_date: date
  termination_date: date
  review_date: date
}

我的搜索界面

Birth: (check box)
Hire: (check box)
Review: (check box)
FROM: Day (select 1-31) Month (select 1-12) Year (text field)
TO: Day (select 1-31) Month (select 1-12) Year (text field)

因此,如果我想查找六月所有雇用的用户。

  

我会选中“雇用”   
我会从“从月份开始”中选择6

如果我想查找今年所有具有评论的用户

  

我会检查“评论”   
我会选择“从2019年开始”(也许是从“ 2019年开始”)

如果我想在7月的前10天找到所有具有出生日期或评论日期的用户

  

我会检查出生   
我会检查评论   
我将选择“从一天开始”的1   
我选择“今天”为10

我当然可以编写IF语句的BUNCH,在查询中添加一行。

类似这样的东西:

var date_range_query = [];    

if ( from_year && to_year ) {
    date_range_query.push({
        $exp: {
            $gte: [ { $year: '$birth_date' }, from_date ]
        }
    });
    date_range_query.push({
        $exp: {
            $lte: [ { $year: '$birth_date' }, to_date ]
        }
    });
} else if ( from_year && !to_year ) {
    date_range_query.push({
        $exp: {
            $eq: [ { $year: '$birth_date' }, from_date ]
        }
    }); 
} else if ( !from_year && to_year ) {
    date_range_query.push({
        $exp: {
            $eq: [ { $year: '$birth_date' }, to_date ]
        }
    });
}

if ( from_month && to_month ) {
    date_range_query.push({
        $exp: {
            $gte: [ { $month: '$birth_date' }, from_date ]
        }
    });
    date_range_query.push({
        $exp: {
            $lte: [ { $month: '$birth_date' }, to_date ]
        }
    });
} else if ( from_month && !to_month ) {
    date_range_query.push({
        $exp: {
            $eq: [ { $month: '$birth_date' }, from_date ]
        }
    })  
} else if ( !from_month && to_month ) {
    date_range_query.push({
        $exp: {
            $eq: [ { $month: '$birth_date' }, to_date ]
        }
    })
}

if ( from_day && to_day ) {
    date_range_query.push({
        $exp: {
            $gte: [ { $day: '$birth_date' }, from_date ]
        }
    });
    date_range_query.push({
        $exp: {
            $lte: [ { $day: '$birth_date' }, to_date ]
        }
    });
} else if ( from_day && !to_day ) {
    date_range_query.push({
        $exp: {
            $eq: [ { $day: '$birth_date' }, from_date ]
        }
    })
} else if ( !from_day && to_day ) {
    date_range_query.push({
        $exp: {
            $eq: [ { $day: '$birth_date' }, to_date ]
        }
    })
}

db.users.find({
    $and: date_range_query
})

结果如下所示(搜索2018年6月或2019年6月出生的人)

db.getCollection('users').find({
    $and: [
        {$expr: {
            $gte: [{ $year: "$birth_date" }, 2018]
        }},
        {$expr: {
            $lte: [{ $year: "$birth_date" }, 2019]
        }},
        {$expr: {
            $eq: [{ $month: "$birth_date" }, 6]
        }}
    ]
 })

但是我有9个IF,并且仅涵盖“生日”。
如果我为这四个日期分别编写一系列IF, 我留下了一个巨大的丑陋的烂摊子

如果有人可以举例说明一个写得更好的查询,它说明了适用于多个日期字段的日期范围-这可能会有所帮助...

这就是我认为我必须在相同的日期范围内搜索“雇用”和“评论”日期的方式-看起来很丑陋和错误:

db.getCollection('users').find({
    $or: [
        {
            $and: [
                {$expr: {
                    $gte: [{ $year: "$hire_date" }, 2018]
                }},
                {$expr: {
                    $lte: [{ $year: "$hire_date" }, 2019]
                }},
                {$expr: {
                    $eq: [{ $month: "$hire_date" }, 6]
                }}
            ]
        },
        {
            $and: [
                {$expr: {
                    $gte: [{ $year: "$review_date" }, 2018]
                }},
                {$expr: {
                    $lte: [{ $year: "$review_date" }, 2019]
                }},
                {$expr: {
                    $eq: [{ $month: "$review_date" }, 6]
                }}
            ]
        }
    ]    
 })

感谢查询帮助,以及有关如何在JS中构建查询以传递给mongooseJS的任何建议

thx

0 个答案:

没有答案