diff --git a/persistence-modules/java-mongodb/pom.xml b/persistence-modules/java-mongodb/pom.xml
index 03229e72bd..88f0d18a5b 100644
--- a/persistence-modules/java-mongodb/pom.xml
+++ b/persistence-modules/java-mongodb/pom.xml
@@ -30,6 +30,18 @@
core
${morphia.version}
+
+ org.testcontainers
+ mongodb
+ 1.16.3
+ test
+
+
+ org.testcontainers
+ junit-jupiter
+ 1.16.3
+ test
+
diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/ordering/caseinsensitive/CaseInsensitiveOrderingLiveTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/ordering/caseinsensitive/CaseInsensitiveOrderingLiveTest.java
new file mode 100644
index 0000000000..ec2c332018
--- /dev/null
+++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/ordering/caseinsensitive/CaseInsensitiveOrderingLiveTest.java
@@ -0,0 +1,108 @@
+package com.baeldung.ordering.caseinsensitive;
+
+import com.mongodb.MongoClient;
+import com.mongodb.client.*;
+import com.mongodb.client.model.Collation;
+import com.mongodb.client.model.Projections;
+import com.mongodb.client.model.Sorts;
+import org.bson.Document;
+import org.bson.conversions.Bson;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.MongoDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+import org.testcontainers.utility.DockerImageName;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static com.mongodb.client.model.Aggregates.project;
+import static com.mongodb.client.model.Aggregates.sort;
+import static com.mongodb.client.model.Sorts.ascending;
+import static org.junit.Assert.assertEquals;
+
+@Testcontainers
+class CaseInsensitiveOrderingLiveTest {
+
+ private static MongoCollection userCollections;
+
+ @Container
+ private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
+
+ @BeforeAll
+ private static void setup() {
+
+ MongoClient mongoClient = new MongoClient(mongoDBContainer.getContainerIpAddress(), mongoDBContainer.getMappedPort(27017));
+ MongoDatabase database = mongoClient.getDatabase("test");
+ userCollections = database.getCollection("users");
+
+ List list = new ArrayList<>();
+ list.add(Document.parse("{'name': 'ben', surname: 'ThisField' }"));
+ list.add(Document.parse("{'name': 'aen', surname: 'Does' }"));
+ list.add(Document.parse("{'name': 'Ben', surname: 'Not' }"));
+ list.add(Document.parse("{'name': 'cen', surname: 'Matter' }"));
+ list.add(Document.parse("{'name': 'Aen', surname: 'Really' }"));
+ list.add(Document.parse("{'name': 'Cen', surname: 'TrustMe' }"));
+
+ userCollections.insertMany(list);
+ }
+
+ @Test
+ void givenMongoCollection_whenUsingFindWithSort_caseIsConsideredByDefault() {
+ FindIterable nameDoc = userCollections.find().sort(ascending("name"));
+ MongoCursor cursor = nameDoc.cursor();
+
+ List expectedNamesOrdering = Arrays.asList("Aen", "Ben", "Cen", "aen", "ben", "cen");
+ List actualNamesOrdering = new ArrayList<>();
+ while (cursor.hasNext()) {
+ Document document = cursor.next();
+ actualNamesOrdering.add(document.get("name").toString());
+ }
+
+ assertEquals(expectedNamesOrdering, actualNamesOrdering);
+ }
+
+ @Test
+ void givenMongoCollection_whenUsingFindWithSortAndCollation_caseIsNotConsidered() {
+ FindIterable nameDoc = userCollections.find().sort(ascending("name"))
+ .collation(Collation.builder().locale("en").build());
+ MongoCursor cursor = nameDoc.cursor();
+ List expectedNamesOrdering = Arrays.asList("aen", "Aen", "ben", "Ben", "cen", "Cen");
+ List actualNamesOrdering = new ArrayList<>();
+ while (cursor.hasNext()) {
+ Document document = cursor.next();
+ actualNamesOrdering.add(document.get("name").toString());
+ }
+
+ assertEquals(expectedNamesOrdering, actualNamesOrdering);
+
+ }
+
+ @Test
+ void givenMongoCollection_whenUsingFindWithSortAndAggregate_caseIsNotConsidered() {
+
+ Bson projectBson = project(
+ Projections.fields(
+ Projections.include("name", "surname"),
+ Projections.computed("lowerName", Projections.computed("$toLower", "$name"))));
+
+ AggregateIterable nameDoc = userCollections.aggregate(
+ Arrays.asList(projectBson,
+ sort(Sorts.ascending("lowerName"))));
+
+ MongoCursor cursor = nameDoc.cursor();
+
+ List expectedNamesOrdering = Arrays.asList("aen", "Aen", "ben", "Ben", "cen", "Cen");
+ List actualNamesOrdering = new ArrayList<>();
+ while (cursor.hasNext()) {
+ Document document = cursor.next();
+ actualNamesOrdering.add(document.get("name").toString());
+ }
+
+ assertEquals(expectedNamesOrdering, actualNamesOrdering);
+ }
+
+
+}