Skip to content

Commit 2809f24

Browse files
author
jmarkerink
committed
feat: optimized aggregation pipeline to use indexes when possible
1 parent e09dcfb commit 2809f24

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Aggregation.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ public static Aggregation fromPipeline(List<Document> pipeline, DatabaseResolver
189189
}
190190

191191
private List<Document> runStages() {
192-
return runStages(collection.queryAllAsStream());
192+
return stages.stream()
193+
.filter(stage -> stage instanceof MatchStage)
194+
.findFirst()
195+
.map(match -> runStages(collection.handleQueryAsStream(((MatchStage) match).getQuery())))
196+
.orElseGet(() -> runStages(collection.queryAllAsStream()));
193197
}
194198

195199
public List<Document> runStages(Stream<Document> stream) {

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/MatchStage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ public String name() {
2424
public Stream<Document> apply(Stream<Document> stream) {
2525
return stream.filter(document -> queryMatcher.matches(document, query));
2626
}
27+
28+
public Document getQuery() {
29+
return query;
30+
}
2731
}

0 commit comments

Comments
 (0)