Skip to content

Commit a3d1ac9

Browse files
committed
feat: fix sort at mongodb
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
1 parent ebe6306 commit a3d1ac9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public Stream<CommunicationEntity> select(SelectQuery query) {
159159

160160
FindIterable<Document> documents = collection.find(mongoDBQuery);
161161
documents.projection(Projections.include(query.columns()));
162+
163+
if (!query.sorts().isEmpty()) {
164+
documents.sort(sort(query.sorts()));
165+
}
166+
162167
if (query.skip() > 0) {
163168
documents.skip((int) query.skip());
164169
}
@@ -167,8 +172,6 @@ public Stream<CommunicationEntity> select(SelectQuery query) {
167172
documents.limit((int) query.limit());
168173
}
169174

170-
query.sorts().stream().map(this::getSort).forEach(documents::sort);
171-
172175
return stream(documents.spliterator(), false).map(MongoDBUtils::of)
173176
.map(ds -> CommunicationEntity.of(collectionName, ds));
174177

@@ -254,10 +257,15 @@ public Stream<CommunicationEntity> select(String collectionName, Bson filter) {
254257
.map(ds -> CommunicationEntity.of(collectionName, ds));
255258
}
256259

257-
private Bson getSort(Sort<?> sort) {
260+
private Bson sort(Sort<?> sort) {
258261
return sort.isAscending() ? Sorts.ascending(sort.property()) : Sorts.descending(sort.property());
259262
}
260263

264+
private Bson sort(List<Sort<?>> sorts) {
265+
List<Bson> bsonSorts = sorts.stream().map(this::sort).toList();
266+
return Sorts.orderBy(bsonSorts);
267+
}
268+
261269
/**
262270
* Returns the number of documents in the collection that match the given query filter.
263271
*

0 commit comments

Comments
 (0)