Skip to content

Commit 4453429

Browse files
authored
Merge pull request #279 from eclipse/include-between-support-mongodb
Include between support mongodb
2 parents d28b6f0 + ebe6306 commit 4453429

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

CHANGELOG.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
88

99
== [Unreleased]
1010

11+
=== Added
12+
13+
- Include between query support at MongoDB
14+
1115
== [1.1.1] - 2023-05-25
1216

1317
=== Changed

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/DocumentQueryConversor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public static Bson convert(CriteriaCondition condition) {
5656
});
5757
yield Filters.or(orList.stream()
5858
.map(DocumentQueryConversor::convert).toList());
59+
}case BETWEEN -> {
60+
List<Object> betweenList = ValueUtil.convertToList(document.value());
61+
yield Filters.and(Filters.gte(document.name(), betweenList.get(0)),
62+
Filters.lte(document.name(), betweenList.get(1)));
63+
5964
}
6065
default -> throw new UnsupportedOperationException("The condition " + condition.condition()
6166
+ " is not supported from mongoDB diana driver");

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManagerTest.java

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,47 @@ void shouldFindDocumentIn() {
266266
assertEquals(entities, entityManager.select(query).collect(Collectors.toList()));
267267
}
268268

269+
@Test
270+
void shouldFindBetween() {
271+
var deleteQuery = delete().from(COLLECTION_NAME).where("type").eq("V").build();
272+
entityManager.delete(deleteQuery);
273+
Iterable<CommunicationEntity> entitiesSaved = entityManager.insert(getEntitiesWithValues());
274+
275+
var query = select().from(COLLECTION_NAME)
276+
.where("age").between(22, 23)
277+
.build();
278+
279+
var result = entityManager.select(query).toList();
280+
281+
SoftAssertions.assertSoftly(softly -> {
282+
softly.assertThat(result).hasSize(2);
283+
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).contains(22, 23);
284+
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).doesNotContain(25);
285+
});
286+
}
287+
288+
@Test
289+
void shouldFindBetween2() {
290+
var deleteQuery = delete().from(COLLECTION_NAME).where("type").eq("V").build();
291+
entityManager.delete(deleteQuery);
292+
Iterable<CommunicationEntity> entitiesSaved = entityManager.insert(getEntitiesWithValues());
293+
294+
var query = select().from(COLLECTION_NAME)
295+
.where("age").between(22, 23)
296+
.and("type").eq("V")
297+
.build();
298+
299+
var result = entityManager.select(query).toList();
300+
301+
SoftAssertions.assertSoftly(softly -> {
302+
softly.assertThat(result).hasSize(2);
303+
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).contains(22, 23);
304+
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).doesNotContain(25);
305+
});
306+
}
307+
308+
309+
269310
@Test
270311
void shouldFindDocumentStart() {
271312
DeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("type").eq("V").build();
@@ -440,7 +481,7 @@ void shouldCreateEntityByteArray() {
440481
entityManager.insert(entity);
441482

442483
List<CommunicationEntity> entities = entityManager.select(select().from("download")
443-
.where("_id").eq(id).build()).collect(Collectors.toList());
484+
.where("_id").eq(id).build()).toList();
444485

445486
assertEquals(1, entities.size());
446487
CommunicationEntity documentEntity = entities.get(0);
@@ -481,6 +522,7 @@ void shouldConvertFromListDocumentList() {
481522
assertDoesNotThrow(() -> entityManager.insert(entity));
482523
}
483524

525+
@SuppressWarnings("unchecked")
484526
@Test
485527
void shouldRetrieveListDocumentList() {
486528
CommunicationEntity entity = entityManager.insert(createDocumentList());
@@ -565,6 +607,7 @@ void shouldUpdateNull(){
565607
});
566608
}
567609

610+
568611
private CommunicationEntity createDocumentList() {
569612
CommunicationEntity entity = CommunicationEntity.of("AppointmentBook");
570613
entity.add(Element.of("_id", new Random().nextInt()));
@@ -594,23 +637,24 @@ private CommunicationEntity getEntity() {
594637
}
595638

596639
private List<CommunicationEntity> getEntitiesWithValues() {
597-
CommunicationEntity lucas = CommunicationEntity.of(COLLECTION_NAME);
640+
var lucas = CommunicationEntity.of(COLLECTION_NAME);
598641
lucas.add(Element.of("name", "Lucas"));
599642
lucas.add(Element.of("age", 22));
600643
lucas.add(Element.of("location", "BR"));
601644
lucas.add(Element.of("type", "V"));
602645

603-
CommunicationEntity otavio = CommunicationEntity.of(COLLECTION_NAME);
646+
var luna = CommunicationEntity.of(COLLECTION_NAME);
647+
luna.add(Element.of("name", "Luna"));
648+
luna.add(Element.of("age", 23));
649+
luna.add(Element.of("location", "US"));
650+
luna.add(Element.of("type", "V"));
651+
652+
var otavio = CommunicationEntity.of(COLLECTION_NAME);
604653
otavio.add(Element.of("name", "Otavio"));
605654
otavio.add(Element.of("age", 25));
606655
otavio.add(Element.of("location", "BR"));
607656
otavio.add(Element.of("type", "V"));
608657

609-
CommunicationEntity luna = CommunicationEntity.of(COLLECTION_NAME);
610-
luna.add(Element.of("name", "Luna"));
611-
luna.add(Element.of("age", 23));
612-
luna.add(Element.of("location", "US"));
613-
luna.add(Element.of("type", "V"));
614658

615659
return asList(lucas, otavio, luna);
616660
}

0 commit comments

Comments
 (0)