简体   繁体   English

查询之间的mongo jpa

[英]mongo jpa between query

I'm using spring jpa to integrate with a mongo database.我正在使用 spring jpa 与 mongo 数据库集成。 I've got a simple mongo document with an Instance field.我有一个带有Instance字段的简单 mongo 文档。 In my repository I've tried something like在我的存储库中,我尝试过类似的东西

findByInstantBetween(Instant start, Instant end, Pageable pageable);

I've got a simple spring unit test (run with spring runner and a normal spring context) that looks like this:我有一个简单的 spring 单元测试(使用 spring 跑步者和普通 spring 上下文运行),如下所示:

import static org.assertj.core.api.Assertions.assertThat;

...


@Test
public void testSearchByInstantBetween() {
    // insert a document before the date range to test, should not be retrieved by query
    MyDocument doc = new MyDocument();
    doc.setInstant(Instant.now());
    doc= docRepository.insert(doc);

    // insert a document with the start instant, should be retrieved
    doc.setId(null);
    Instant start = Instant.now();
    doc.setInstant(start);
    doc = docRepository.insert(doc);

    // insert a document with the end instant, should be retrieved
    doc.setId(null);
    Instant end = Instant.now();
    doc.setInstant(end);
    doc = docRepository.insert(doc);

    // insert a document after the end instant, should not be retrieved
    doc.setId(null);
    doc.setInstant(Instant.now());
    doc = docRepository.insert(doc);

    // check that 4 documents have been inserted
    assertThat(docRepository.findAll()).hasSize(4);

    Pageable pageable = PageRequest.of(0, 5);

    // run between query, expected size is 2
    Page<MyDocument> docs = docRepository.findByInstantBetween(start, end, pageable);
    assertThat(docs.getContent()).hasSize(2); // <-- this fails with expected 2, found 0
}

I've also tried to change the query method as follows我也试过改变查询方法如下

findByInstantGreaterThanEqualAndInstantLessThanEqual(Instant start, Instant end, Pageable pageable);

And when I run the test with this method I get当我用这种方法运行测试时,我得到

org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'instant' expression specified as 'instant: Document{{$lte=2019-10-15T06:28:43.508Z}}'. Criteria already contains 'instant : Document{{$gte=2019-10-15T06:28:43.505Z}}'.

Has anyone had this kind of problem?有没有人遇到过这种问题?

The Between query method was working correctly.查询方法Between工作正常。 I didn't know it was exclusive, so since I only had a test case with two elements ( start and end Instant s respectively) the query looked between (not including) those and found 0 elements.我不知道它是排他的,所以因为我只有一个包含两个元素(分别为startend Instant s)的测试用例,所以查询在(不包括)这些元素之间查找并找到了 0 个元素。

When I added another element with an Instant in the middle of start and end , I expected 3 but it returned 1.当我在startend中间添加另一个带有Instant的元素时,我期望 3 但它返回 1。

In order to make the query inclusive I annotated it as follows为了使查询具有包容性,我将其注释如下

@org.springframework.data.mongodb.repository.Query("{ instant: {$gte: ?0, $lte: ?1}}")

where ?0 and ?1 correspond with the start and end parameters respectively.其中?0?1分别对应于startend参数。

Hopefully this can help someone else.希望这可以帮助别人。

Use below example query for finding something with greater or equal and less than or equal.使用下面的示例查询来查找大于或等于和小于或等于的东西。

@Query("{instant : {$gte : ?0, $lte : ?1}}")
 findUsingQ(Instant start, Instant end, Pageable pageable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM