如何使用Java在mongodb中通过设置查询对象而不是匹配操作来执行聚合?

如何使用Java在mongodb中通过设置查询对象而不是匹配操作来执行聚合?,java,mongodb,spring-boot,Java,Mongodb,Spring Boot,代码如下: Criteria criteria=null; criteria = new Criteria().andOperator(Criteria.where("OwnerId").is(ownerId), Criteria.where("Environment").is(env), Criteria.where("ServiceName").in(api));

代码如下:

            Criteria criteria=null;
            criteria = new Criteria().andOperator(Criteria.where("OwnerId").is(ownerId), Criteria.where("Environment").is(env), Criteria.where("ServiceName").in(api));
            MatchOperation matchOp= Aggregation.match(criteria);
            ProjectionOperation projOp= Aggregation.project("SubServices").andInclude("ServerGroupName").andInclude("NodeName").andInclude("ServiceVersion")
                    .andExclude("_id");
            Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
            AggregationResults<Document> aggregate = mongoOperations.aggregate(aggr, "CNF_SERVICE",Document.class,"config",env);
Criteria=null;
criteria=new criteria().andOperator(criteria.where(“OwnerId”)是(OwnerId),criteria.where(“Environment”)是(env),criteria.where(“ServiceName”).in(api));
MatchOperation matchOp=聚合。匹配(标准);
ProjectionOperation projOp=Aggregation.project(“子服务”).andInclude(“服务器组名”).andInclude(“节点名”).andInclude(“服务版本”)
.并排除(“_id”);
Aggregation aggr=Aggregation.newAggregation(matchOp,projOp);
AggregationResults aggregate=mongoOperations.aggregate(aggr,“CNF_服务”,Document.class,“config”,env);
上面的代码运行良好。但是,为了动态生成条件,我需要进行以下更改:

            Criteria criteria=new Criteria();
            Query query= new Query();
            if(ownerId!=null && ownerId>0) {
                query.addCriteria(criteria.andOperator(Criteria.where("OwnerId").is(ownerId)));
            }
            if(env!=null) {
                query.addCriteria(criteria.andOperator(Criteria.where("Environment").is(env)));
            }
            if(api!=null) {
                query.addCriteria(criteria.andOperator(Criteria.where("ServiceName").in(api)));
            }
            MatchOperation matchOp= Aggregation.match("How to set the query here?");
            ProjectionOperation projOp= Aggregation.project("SubServices").andInclude("ServerGroupName").andInclude("NodeName").andInclude("ServiceVersion")
                    .andExclude("_id");
            Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
            AggregationResults<Document> aggregate = mongoOperations.aggregate(aggr, "CNF_SERVICE",Document.class,"config",env);
标准=新标准();
查询=新查询();
如果(ownerId!=null&&ownerId>0){
query.addCriteria(criteria.andOperator(criteria.where(“OwnerId”).is(OwnerId));
}
如果(env!=null){
query.addCriteria(criteria.andOperator(criteria.where(“Environment”).is(env));
}
如果(api!=null){
query.addCriteria(criteria.andOperator(criteria.where(“ServiceName”).in(api));
}
MatchOperation matchOp=Aggregation.match(“如何在此处设置查询?”);
ProjectionOperation projOp=Aggregation.project(“子服务”).andInclude(“服务器组名”).andInclude(“节点名”).andInclude(“服务版本”)
.并排除(“_id”);
Aggregation aggr=Aggregation.newAggregation(matchOp,projOp);
AggregationResults aggregate=mongoOperations.aggregate(aggr,“CNF_服务”,Document.class,“config”,env);

我需要将查询设置为条件,并将其应用于匹配操作。如何实现这一点?

您可以实现以下代码,而不是匹配操作: 希望这会有帮助

标准=新标准(); 查询=新查询();query.addCriteria(criteria.andOperator(criteria.where(“OwnerId”).is(OwnerId));query.fields().include(“子服务”);query.fields().include(“ServerGroupName”);query.fields().exclude(“_id”); 查找(查询、映射、类、集合名)