Skip to content

Commit fb60c67

Browse files
committed
Create a new Collection with the projection class
1 parent 3e2b877 commit fb60c67

File tree

10 files changed

+60
-20
lines changed

10 files changed

+60
-20
lines changed

extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/reactive/ReactiveMongoCollection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.util.List;
44

5-
import com.mongodb.client.MongoCollection;
65
import org.bson.Document;
76
import org.bson.codecs.configuration.CodecRegistry;
87
import org.bson.conversions.Bson;
98

109
import com.mongodb.MongoNamespace;
1110
import com.mongodb.bulk.BulkWriteResult;
11+
import com.mongodb.client.MongoCollection;
1212
import com.mongodb.client.model.BulkWriteOptions;
1313
import com.mongodb.client.model.CountOptions;
1414
import com.mongodb.client.model.CreateIndexOptions;
@@ -1492,7 +1492,8 @@ Uni<Void> renameCollection(ClientSession clientSession, MongoNamespace newCollec
14921492
CodecRegistry getCodecRegistry();
14931493

14941494
/**
1495-
* Create a new ReactiveMongoCollection instance with a different default class to cast any documents returned from the database into..
1495+
* Create a new ReactiveMongoCollection instance with a different default class to cast any documents returned from the
1496+
* database into..
14961497
*
14971498
* @param clazz the default class to cast any documents returned from the database into.
14981499
* @param <NewTDocument> The type that the new collection will encode documents from and decode documents to

extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/reactive/runtime/ReactivePanacheQueryImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class ReactivePanacheQueryImpl<Entity> implements ReactivePanacheQuery<En
3838
this.sort = sort;
3939
}
4040

41-
private ReactivePanacheQueryImpl(ReactivePanacheQueryImpl previousQuery, Bson projections) {
42-
this.collection = previousQuery.collection;
41+
private ReactivePanacheQueryImpl(ReactivePanacheQueryImpl previousQuery, Bson projections, Class<?> type) {
42+
this.collection = previousQuery.collection.withDocumentClass(type);
4343
this.mongoQuery = previousQuery.mongoQuery;
4444
this.sort = previousQuery.sort;
4545
this.projections = projections;
@@ -62,7 +62,7 @@ public <T> ReactivePanacheQuery<T> project(Class<T> type) {
6262
projections.append(fieldName, 1);
6363
}
6464

65-
return new ReactivePanacheQueryImpl(this, projections);
65+
return new ReactivePanacheQueryImpl(this, projections, type);
6666
}
6767

6868
@Override

extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/PanacheQueryImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class PanacheQueryImpl<Entity> implements PanacheQuery<Entity> {
3838
this.sort = sort;
3939
}
4040

41-
private PanacheQueryImpl(PanacheQueryImpl previousQuery, Bson projections) {
42-
this.collection = previousQuery.collection;
41+
private PanacheQueryImpl(PanacheQueryImpl previousQuery, Bson projections, Class<?> documentClass) {
42+
this.collection = previousQuery.collection.withDocumentClass(documentClass);
4343
this.mongoQuery = previousQuery.mongoQuery;
4444
this.sort = previousQuery.sort;
4545
this.projections = projections;
@@ -62,7 +62,7 @@ public <T> PanacheQuery<T> project(Class<T> type) {
6262
projections.append(fieldName, 1);
6363
}
6464

65-
return new PanacheQueryImpl(this, projections);
65+
return new PanacheQueryImpl<>(this, projections, type);
6666
}
6767

6868
@Override

integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/person/PersonEntityResource.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.quarkus.it.mongodb.panache.person;
22

33
import java.net.URI;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import javax.ws.rs.*;
79
import javax.ws.rs.core.MediaType;
@@ -23,8 +25,11 @@ public List<PersonEntity> getPersons(@QueryParam("sort") String sort) {
2325

2426
@GET
2527
@Path("/search/{name}")
26-
public List<PersonName> searchPersons(@PathParam("name") String name) {
27-
return PersonEntity.find("lastname", name).project(PersonName.class).list();
28+
public Set<PersonName> searchPersons(@PathParam("name") String name) {
29+
Set<PersonName> uniqueNames = new HashSet<>();
30+
List<PersonName> lastnames = PersonEntity.find("lastname", name).project(PersonName.class).list();
31+
lastnames.forEach(p -> uniqueNames.add(p));// this will throw if it's not the right type
32+
return uniqueNames;
2833
}
2934

3035
@POST
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package io.quarkus.it.mongodb.panache.person;
22

3+
import java.util.Objects;
4+
35
public class PersonName {
46
public String lastname;
7+
8+
@Override
9+
public boolean equals(Object o) {
10+
if (this == o)
11+
return true;
12+
if (o == null || getClass() != o.getClass())
13+
return false;
14+
PersonName that = (PersonName) o;
15+
return Objects.equals(lastname, that.lastname);
16+
}
17+
18+
@Override
19+
public int hashCode() {
20+
return Objects.hash(lastname);
21+
}
522
}

integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/person/PersonRepositoryResource.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.quarkus.it.mongodb.panache.person;
22

33
import java.net.URI;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import javax.inject.Inject;
79
import javax.ws.rs.*;
@@ -32,8 +34,11 @@ public List<Person> getPersons(@QueryParam("sort") String sort) {
3234

3335
@GET
3436
@Path("/search/{name}")
35-
public List<PersonName> searchPersons(@PathParam("name") String name) {
36-
return personRepository.find("lastname", name).project(PersonName.class).list();
37+
public Set<PersonName> searchPersons(@PathParam("name") String name) {
38+
Set<PersonName> uniqueNames = new HashSet<>();
39+
List<PersonName> lastnames = personRepository.find("lastname", name).project(PersonName.class).list();
40+
lastnames.forEach(p -> uniqueNames.add(p));// this will throw if it's not the right type
41+
return uniqueNames;
3742
}
3843

3944
@POST

integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/reactive/person/ReactivePersonEntityResource.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.quarkus.it.mongodb.panache.reactive.person;
22

33
import java.net.URI;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import javax.ws.rs.*;
79
import javax.ws.rs.core.MediaType;
@@ -25,8 +27,12 @@ public Uni<List<ReactivePersonEntity>> getPersons(@QueryParam("sort") String sor
2527

2628
@GET
2729
@Path("/search/{name}")
28-
public Uni<List<PersonName>> searchPersons(@PathParam("name") String name) {
29-
return ReactivePersonEntity.find("lastname", name).project(PersonName.class).list();
30+
public Set<PersonName> searchPersons(@PathParam("name") String name) {
31+
Set<PersonName> uniqueNames = new HashSet<>();
32+
List<PersonName> lastnames = ReactivePersonEntity.find("lastname", name).project(PersonName.class).list().await()
33+
.indefinitely();
34+
lastnames.forEach(p -> uniqueNames.add(p));// this will throw if it's not the right type
35+
return uniqueNames;
3036
}
3137

3238
@POST

integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/reactive/person/ReactivePersonRepositoryResource.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.quarkus.it.mongodb.panache.reactive.person;
22

33
import java.net.URI;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import javax.inject.Inject;
79
import javax.ws.rs.*;
@@ -31,8 +33,12 @@ public Uni<List<Person>> getPersons(@QueryParam("sort") String sort) {
3133

3234
@GET
3335
@Path("/search/{name}")
34-
public Uni<List<PersonName>> searchPersons(@PathParam("name") String name) {
35-
return reactivePersonRepository.find("lastname", name).project(PersonName.class).list();
36+
public Set<PersonName> searchPersons(@PathParam("name") String name) {
37+
Set<PersonName> uniqueNames = new HashSet<>();
38+
List<PersonName> lastnames = reactivePersonRepository.find("lastname", name).project(PersonName.class).list().await()
39+
.indefinitely();
40+
lastnames.forEach(p -> uniqueNames.add(p));// this will throw if it's not the right type
41+
return uniqueNames;
3642
}
3743

3844
@POST

integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/MongodbPanacheResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private void callPersonEndpoint(String endpoint) {
257257

258258
//with project
259259
list = get(endpoint + "/search/Doe").as(LIST_OF_PERSON_TYPE_REF);
260-
Assertions.assertEquals(2, list.size());
260+
Assertions.assertEquals(1, list.size());
261261
Assertions.assertNotNull(list.get(0).lastname);
262262
//expected the firstname field to be null as we project on lastname only
263263
Assertions.assertNull(list.get(0).firstname);
@@ -270,7 +270,7 @@ private void callPersonEndpoint(String endpoint) {
270270
.when().post(endpoint + "/rename")
271271
.then().statusCode(200);
272272
list = get(endpoint + "/search/Dupont").as(LIST_OF_PERSON_TYPE_REF);
273-
Assertions.assertEquals(2, list.size());
273+
Assertions.assertEquals(1, list.size());
274274

275275
//count
276276
Long count = get(endpoint + "/count").as(Long.class);

integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/reactive/ReactiveMongodbPanacheResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private void callReactivePersonEndpoint(String endpoint) {
279279

280280
//with project
281281
list = get(endpoint + "/search/Doe").as(LIST_OF_PERSON_TYPE_REF);
282-
Assertions.assertEquals(2, list.size());
282+
Assertions.assertEquals(1, list.size());
283283
Assertions.assertNotNull(list.get(0).lastname);
284284
//expected the firstname field to be null as we project on lastname only
285285
Assertions.assertNull(list.get(0).firstname);
@@ -292,7 +292,7 @@ private void callReactivePersonEndpoint(String endpoint) {
292292
.when().post(endpoint + "/rename")
293293
.then().statusCode(200);
294294
list = get(endpoint + "/search/Dupont").as(LIST_OF_PERSON_TYPE_REF);
295-
Assertions.assertEquals(2, list.size());
295+
Assertions.assertEquals(1, list.size());
296296

297297
//count
298298
Long count = get(endpoint + "/count").as(Long.class);

0 commit comments

Comments
 (0)