【问题标题】:How to filter a query on specific date format in mongodb?如何在 mongodb 中过滤特定日期格式的查询?
【发布时间】:2017-04-20 10:36:20
【问题描述】:

我在string format 'yyyyMMdd' 中收到date parameter

我在 mongodb 中有一个集合,它有一个属性 CallDate ISODate(),我需要写下一个查询来过滤日期参数。

SQL Server中,我们只使用查询

select * from xyz x with (nolock) 
where CONVERT(VARCHAR(23), Cast(x.CallDate as datetime), 112)  = @pDateParameter

如何在 mongodb

中实现相同的过滤器
db.getCollection("_xyz").find({CallDate : ?}) //How to adjust that filter here

【问题讨论】:

    标签: sql-server mongodb


    【解决方案1】:

    您可以在 3.4 版本中尝试以下聚合。

    使用$addFields 添加字符串格式的日期属性,后跟$match on date 参数,如果您愿意,可以稍后使用$project 排除字符串日期列。

    db.collection.aggregate([
           { $addFields: {yearMonthDay: { $dateToString: { format: "%Y%m%d", date: "$CallDate" } } } },
           { $match: {"yearMonthDay":date parameter}},
           { $project:{"yearMonthDay":0}}
        ])
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2017-10-26
    相关资源
    最近更新 更多