【问题标题】:How to compare the fromdate and todate in Spring MongoDB filter query如何比较 Spring MongoDB 过滤器查询中的 fromdate 和 todate
【发布时间】:2020-07-08 05:43:05
【问题描述】:

我在 fromdate 和 todate 之间从 MongoDB 获取文档。所有文档中都有一个名为“lastActive”的字段。 lastActive 字段是 mongodb 文档中的日期字符串。我需要将 lastActive 字段文档与大于 fromDate 和小于 toDate 进行比较。我不知道如何在 SpringBoot 中编写查找查询,我已经写过,但给出的记录为零。

Active_userlist=profile_collection.find(Filters.and(Filters.gte("lastActive", 
fromDate),Filters.lte("lastActive",toDate))).into(new ArrayList<Document>));

有人可以帮我在日期范围内获得适当的文件吗? 注意:我是用postman调用这个API的,我在postman的ResponseBody中提供了fromdate和todate。

【问题讨论】:

    标签: mongodb spring-boot arraylist


    【解决方案1】:

    您可能正在将日期字段与来自 API 的字符串进行比较。将日期从字符串转换为日期对象。

    例如:

    
    String DB_FORMAT_DATETIME = "yyyy-M-d HH:mm:ss"
    
    DateFormat formatter = new SimpleDateFormat(DB_FORMAT_DATETIME);
    
    formatter.parse(dateStr); // dateStr is string date received from API
    
    

    【讨论】:

    • 你能帮我查询一下吗
    • 你在spring data中使用mongo模板与MongoDB交互吗?
    • 您好,我正在使用 MongoRepository 与 MongoDB 进行交互
    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多