首页 > 解决方案 > 在 mongoDB 中查找以 16:xx:xx 开头的集合中的元素

问题描述

如果我想获取日期为 16:xx:xx 的元素 db.collection.find({"Date":/^16:/}) 以下元素应为:2020-09-24 16:00 :00 2020-06-10 16:01:30 2019-02-14 16:38:00 我怎样才能得到这样的结果?

标签: mongodb

解决方案


作为解决方案,您可以在搜索之前将日期投影到字符串,之后您可以轻松使用正则表达式来实现搜索,这是简单的示例

db.collection.aggregate(
[
    {
        $project: {
            "d":  { $dateToString: {format: "%Y-%m-%d", date: "$mydate"}},key:1
        }
    }  ,
    {
        $match: {
            d: /^16:/i
        }
    }
]
)

推荐阅读