【问题标题】:spring mongoRepository find by timespring mongoRepository 按时间查找
【发布时间】:2020-11-29 03:15:40
【问题描述】:

我在 mongoDB 中有以下查询

  db.item.find({"startdate":{"$gte" : (new Date(itemDate)).getTime()}})

其中 itemDate 是 YYYY-MM-DD 格式的日期

  itemDate="2017-01-01"

现在我想在一个 springBoot 应用程序中,在一个扩展 mongoRepository 的类中编写它。

我试过了:

   `itemRepository.find({"itemDate":{"$gte" : (new Date(itemDate)).getTime()}})`;

但我什至无法编译它。 我不知道要使用哪种 mongoRepository 方法。 字段 startdate 似乎存储为时间戳。

【问题讨论】:

    标签: mongodb spring-boot mongodb-query


    【解决方案1】:

    MongoTemplate 可能更适合您的需求。当有像这样的更复杂的查询时,最好使用它。使用 MongoTemplate,您可以这样编写查询:

    Query query = Query.query(Criteria.where("startdate").gte(new Date(itemDate).getTime()));
    mongoTemplate.find(query, Item.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多