java - 为什么 CustomProjectAggregationOperation 在 SpringMongoTemplate 中不起作用

标签 java mongodb spring-data aggregation-framework spring-mongo

我在 spring 中尝试在模板上执行以下查询,该查询在 Mongoserver 中工作,但是当我在 SpringMongo 模板中使用查询时,过滤器不起作用,它获取所有日期,而不是过滤器日期。如果有人知道这个问题。请帮我解决这个问题。

native Mongo 查询:-

db.Task_Status.aggregate([{
{"$addFields":{"timeDiff": { $floor: { $divide: [ { $subtract:[ new Date() , "$currenttime" ] } , 60000 ] }  } }},
{$match:{$and:[{"timeDiff":{"$lt": 12}}]}} }]);

Spring MongoTempalte java 代码:

String nativeFilterQuery = "{\"$addFields\":{\"timeDiff\": { $floor: { $divide: [ { $subtract:[ new Date() , \"$currenttime\" ] } , 60000 ] }  } }},{$match:{$and:[{\"timeDiff\":{\"$lt\": 12}}]}}";
List<TaskStatus> amTaskLists = Collections.EMPTY_LIST;
        List<AggregationOperation> aggregationOperations = new ArrayList<>();
        if (StringUtility.isNotNullOrEmpty(nativeFilterQuery)) {
            aggregationOperations.add(new CustomProjectAggregationOperation(nativeFilterQuery));
        }
        TypedAggregation<TaskStatus> aggregation = Aggregation.newAggregation(TaskStatus.class, aggregationOperations);
        AggregationResults result = execute(aggregation, TaskStatus.class);
        amTaskLists = result.getMappedResults();

最佳答案

当我拆分查询并按照其工作原理运行时

String queryOne = "{\"$addFields\":{\"timeDiff\": { $floor: { $divide: [ { $subtract:[ new Date() , \"$currenttime\" ] } , 60000 ] }  } }}";
            String queryTwo = "{$match:{$and:[{\"timeDiff\":{\"$lt\": 12}}]}}";
            List<AggregationOperation> aggregationOperations = new ArrayList<>();
            if (StringUtility.isNotNullOrEmpty(queryOne)) {
                aggregationOperations.add(new CustomProjectAggregationOperation(queryOne));
            }
            if (StringUtility.isNotNullOrEmpty(queryTwo)) {
                aggregationOperations.add(new CustomProjectAggregationOperation(queryTwo));
            }

关于java - 为什么 CustomProjectAggregationOperation 在 SpringMongoTemplate 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62166253/

相关文章:

java - 为什么jList中的selectedIndex比集合大小大?

java - 使用 Microsoft JDBC 驱动程序受 SELECT INTO 查询影响的行数

Spring-data-redis ping 有效, key 存在,没有返回数据

java - 使用 Spring 从 Mongo 检索任何对象的最佳方法是什么?

regex - MongoDB $regex 查询和潜在利用

java - 从 Spring Data Jpa 查询返回自定义对象

spring-data-jpa - JpaRepository 在 Spring Boot 2.6.5 中抛出异常

java - eclipse EE Juno VS Glassfish错误: glassfish\domains\domain1 does not exist

java - 使用maven出现异常

mongodb - 对我的案例MongoDB模式设计的看法