Migrate Java Code from Spring Data MongoDB to MongoDB Driver Reactive Streams - A Step-by-Step Guide
The migration from spring-data-mongodb to pure java mongodb-driver-reactivestreams involves changing the method calls and syntax of the code. Here are the steps to migrate the code:
-
Replace the
mongoTemplate.aggregate()method withFlux.from(dbClient.getCollection().aggregate())method. -
Replace the
Aggregation.newAggregation()method withArrays.asList()method. -
Replace
Aggregation.match()method withAggregates.match()method and useFiltersto specify the filter criteria. -
Replace
Aggregation.lookup()method withAggregates.lookup()method and specify the collection name, local field, foreign field, and output field usingDocument.parse()method. -
Replace
Aggregation.project()method withAggregates.project()method and specify the projection fields usingProjections.fields()andProjections.computed()methods. -
Replace
Aggregation.group()method withAggregates.group()method and specify the group by fields usingnew Document()method andAccumulatorsusingAccumulators.*()methods. -
Replace
Aggregation.sort()method withAggregates.sort()method and specify the sort direction usingSorts.*()methods. -
Replace
Aggregation.unwind()method withAggregates.unwind()method and specify the unwind field using'$'symbol. -
Replace
new AggregationOperation()method withnew Document()method and specify the operation using'$match'and'$expr'symbols. -
Replace the use of
CriteriaandComparisonOperatorswithFiltersand useDocument.parse()method to specify the filter criteria. -
Use
Projections.excludeId()method to exclude the'_id'field from the output. -
Use
AggregationOptions.builder().allowDiskUse(true).build()method to enable disk use for large aggregation operations. -
Use
getUniqueMappedResult()method to get the unique result from the aggregation pipeline.
Here is the migrated code for Example 2:
@GetMapping("/frontendOverallStat_callPerPage")
public Object frontendOverallStat_callPerPage(
@RequestParam(name = "projectId", required = false) String projectId,
@RequestParam(name = "submoduleId", required = false) Integer submoduleId
) {
return Optional.ofNullable(Flux.from(dbClient.getCollection(kapokMongoProvider.getMongoBreadcrumbsCollectionName(projectId)).aggregate(Arrays.asList(
KapokLogPresenterApiController.optionalMatchSubmoduleId(submoduleId),
Aggregates.match(
Filters.and(
Filters.eq("category", "xhr"),
Filters.exists("data.url"),
Filters.ne("data.url", ""),
Filters.exists("timestamp"),
Filters.gt("timestamp", (double)(OffsetDateTime.now().minusDays(1).toInstant().toEpochMilli()) / 1000)
)
),
Aggregates.lookup(
kapokMongoProvider.getMongoBreadcrumbsCollectionName(projectId),
"event_id",
"event_id",
new Document("$arrayElemAt", Arrays.asList("$breadcrumbObjects", 0))
),
Aggregates.unwind("$breadcrumbObjects"),
new Document("$match",
new Document("$expr",
new Document("$gt", Arrays.asList("$timestamp", "$breadcrumbObjects.timestamp"))
)
),
Aggregates.sort(Sorts.descending("breadcrumbObjects.timestamp")),
Aggregates.match(Filters.eq("breadcrumbObjects.category", "navigation")),
Aggregates.group(new Document("_id", "$_id"),
Accumulators.first("timestamp", "$timestamp"),
Accumulators.first("url", "$data.url"),
Accumulators.first("breadcrumbObject", "$breadcrumbObjects")
),
Aggregates.match(
Filters.and(
Filters.exists("breadcrumbObject.data.to"),
Filters.ne("breadcrumbObject.data.to", "")
)
),
Aggregates.group(new Document("_id", new Document("timestamp", "$timestamp").append("url", "$url")),
Accumulators.first("path", "breadcrumbObject.data.to")
),
Aggregates.group(null,
Accumulators.sum("pathNum", 1),
Accumulators.sum("totalCallNum", "callNum")
),
Aggregates.project(Projections.fields(
Projections.excludeId(),
Projections.computed("callPerPage", new Document("$divide", Arrays.asList("$totalCallNum", "$pathNum")))
))
)).allowDiskUse(true).first()).blockLast())
.orElse(frontendOverallStatDefaultDocument);
}
原文地址: https://www.cveoy.top/t/topic/mJ8X 著作权归作者所有。请勿转载和采集!