Skip to content

Commit b2a9a2f

Browse files
committed
Updating aggregate pipeline to join the two collections on all three important columns: facilityId, resourceType and resourceId
1 parent 6fac0b9 commit b2a9a2f

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

Java/measureeval/src/main/java/com/lantanagroup/link/measureeval/repositories/AbstractResourceRepository.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.lantanagroup.link.measureeval.entities.PatientResource;
66
import com.lantanagroup.link.measureeval.entities.SharedResource;
77
import com.lantanagroup.link.measureeval.records.AbstractResourceRecord;
8+
import com.lantanagroup.link.shared.mongo.CustomAggregationOperation;
9+
import org.bson.Document;
810
import org.hl7.fhir.r4.model.ResourceType;
911
import org.springframework.data.mongodb.core.MongoOperations;
1012
import org.springframework.data.mongodb.core.aggregation.Aggregation;
@@ -123,7 +125,27 @@ public List<PatientResource> findPatientResources(String facilityId, String pati
123125
.and("reports.reportTrackingId").in(reportIds)));
124126
pipeline.add(Aggregation.unwind("resources"));
125127
pipeline.add(Aggregation.match(Criteria.where("resources.normalizationStatus").is("NORMALIZED")));
126-
pipeline.add(Aggregation.lookup("patientResource", "resources.resourceId", "resourceId", "matchedResources"));
128+
129+
Document lookupStage = new Document("$lookup",
130+
new Document("from", "patientResource")
131+
.append("let", new Document("localResourceId", "$resources.resourceId")
132+
.append("localResourceType", "$resources.resourceType"))
133+
.append("pipeline", List.of(
134+
new Document("$match",
135+
new Document("$expr",
136+
new Document("$and", List.of(
137+
new Document("$eq", List.of("$resourceId", "$$localResourceId")),
138+
new Document("$eq", List.of("$resourceType", "$$localResourceType")),
139+
new Document("$eq", List.of("$facilityId", facilityId))
140+
))
141+
)
142+
)
143+
))
144+
.append("as", "matchedResources")
145+
);
146+
147+
pipeline.add(new CustomAggregationOperation(lookupStage));
148+
127149
pipeline.add(Aggregation.unwind("matchedResources"));
128150
pipeline.add(Aggregation.match(Criteria.where("matchedResources.facilityId").is(facilityId)));
129151
pipeline.add(Aggregation.replaceRoot("matchedResources"));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lantanagroup.link.shared.mongo;
2+
3+
import org.bson.Document;
4+
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
5+
6+
public class CustomAggregationOperation implements AggregationOperation {
7+
private final Document operation;
8+
9+
public CustomAggregationOperation(Document operation) {
10+
this.operation = operation;
11+
}
12+
13+
@Override
14+
public Document toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext context) {
15+
return context.getMappedObject(operation);
16+
}
17+
}

0 commit comments

Comments
 (0)