【问题标题】:Date Queries with MongoDB and Spring Data MongoDB使用 MongoDB 和 Spring Data MongoDB 进行日期查询
【发布时间】:2019-01-04 04:04:27
【问题描述】:

我想搜索预订开始日期和预订结束日期在特定日期范围内的预订列表。

如果我在 MySQL 中查询它会是这样的:

MySQL:

SELECT * FROM MotelReservations
WHERE motelId = 'ABC123' AND (
  (dateStart BETWEEN '2018-12-01 00:00:00' AND '2018-12-31 23:59:59') OR (dateEnd BETWEEN '2018-12-01 00:00:00' AND '2018-12-31 23:59:59')
)

但我不确定 mongodb 的查询相当于什么。

MongoDB:

{
  "motelId": "ABC123",
  ???
}

另外,因为我将使用 Spring Data MongoDB 专门的存储库方法。我想使用类似 JPA 的样式来查询它,我可以只使用 findAllBy... 方法名称。如果不可能,我可以使用@Query 注释来解决。问题是,我不确定 mongo-db 中的查询相当于什么。

Spring Data MongoDB(存储库):

MotelReservationDao接口中,通过方法名findAllBy...或者使用查询注解搜索

findAllByMotelIdAnd...(String motelId, ...)

@Query(???)
findAllReservationsThatFallsOnGivenDates

示例 Mongo 文档:

{
    "reservedBy": "Mang Kanor" ,
    "id":  "b1a7ddd3-ddfd-4624-8e85-79b47fb19f99" ,
    "motelId":  "ABC123" ,
    "dateEnd":  "2018-11-20T10:00:00" ,
    "dateStart":  "2018-11-20T09:00:00" ,
    "summary":  "Enjoy the moment in room 123"
}

【问题讨论】:

  • 你能举一个你的 MongoDB motel 文档的例子吗?
  • 添加了一个示例文档:)

标签: mysql mongodb mongodb-query spring-data-mongodb


【解决方案1】:

MongoDb 中的等效查询

可用于检索示例文档的 MongoDb 查询,

{$and: [
    {'motelId':{$eq:'ABC123'}},
    {$or: [
        {'dateStart':{$gte: '2018-11-01 00:00:00', $lte: '2018-12-31 23:59:59'}},
        {'dateEnd':{$gte: '2018-11-01 00:00:00', $lte: '2018-12-31 23:59:59'}}
    ]}
]}

或者用隐式$and操作符:

{'motelId':{$eq:'ABC123'},
$or: [
    {'dateStart':{$gte: '2018-11-01 00:00:00', $lte: '2018-12-31 23:59:59'}},
    {'dateEnd':{$gte: '2018-11-01 00:00:00', $lte: '2018-12-31 23:59:59'}}
]}

Spring数据mongo等价查询

我不确定您是否可以使用 Spring Data 方法签名创建这个复杂的查询。但您可以查看this section 了解更多信息。

1。使用@Query 注解

@Query("{'motelId':{$eq:?0},$or: [{'dateStart':{$gte:?1, $lte:?2}},{'dateEnd':{$gte: ?3, $lte:?4}}]}")
public List<MotelReservation> findReservationByDate(String motelId, Date from1, Date from2, Date to1, Date to2); 

2。使用MongoTemplate

MotelReservationDAO 界面

public interface MotelReservationDAO extends MongoRepository<MotelReservation, ObjectId>,
        CustomMotelReservationDAO {

}

CustomMotelReservationDAO 接口

public interface CustomMotelReservationDAO {
  public List<MotelReservation> findReservationByDate(String motelId, Date from1, Date from2, Date to1, Date to2); 
}

CustomMotelReservationDAOImpl 类

public class CustomMotelReservationDAOImpl implements CustomMotelReservationDAO {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override    
    public List<MotelReservation> findReservationByDate(String motelId, Date from1, Date from2, Date to1, Date to2){

      Query query = new Query(
        Criteria.where("montelId").is(motelId)
        .andOperator(
          Criteria.where("dateStart").gte(from1).lte(from2),
          Criteria.where("dateEnd").gte(to1).lte(to2)
        )
      );

     return mongoTemplate.find(query, MotelReservation.class)
}

【讨论】:

    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 2017-03-28
    • 2020-12-30
    • 2012-05-05
    • 1970-01-01
    • 2016-12-01
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多