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:

  1. Replace the mongoTemplate.aggregate() method with Flux.from(dbClient.getCollection().aggregate()) method.

  2. Replace the Aggregation.newAggregation() method with Arrays.asList() method.

  3. Replace Aggregation.match() method with Aggregates.match() method and use Filters to specify the filter criteria.

  4. Replace Aggregation.lookup() method with Aggregates.lookup() method and specify the collection name, local field, foreign field, and output field using Document.parse() method.

  5. Replace Aggregation.project() method with Aggregates.project() method and specify the projection fields using Projections.fields() and Projections.computed() methods.

  6. Replace Aggregation.group() method with Aggregates.group() method and specify the group by fields using new Document() method and Accumulators using Accumulators.*() methods.

  7. Replace Aggregation.sort() method with Aggregates.sort() method and specify the sort direction using Sorts.*() methods.

  8. Replace Aggregation.unwind() method with Aggregates.unwind() method and specify the unwind field using '$' symbol.

  9. Replace new AggregationOperation() method with new Document() method and specify the operation using '$match' and '$expr' symbols.

  10. Replace the use of Criteria and ComparisonOperators with Filters and use Document.parse() method to specify the filter criteria.

  11. Use Projections.excludeId() method to exclude the '_id' field from the output.

  12. Use AggregationOptions.builder().allowDiskUse(true).build() method to enable disk use for large aggregation operations.

  13. 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);
}
Migrate Java Code from Spring Data MongoDB to MongoDB Driver Reactive Streams - A Step-by-Step Guide

原文地址: https://www.cveoy.top/t/topic/mJ8X 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录