英文:
How to perform aggregation in mongodb using Java by setting query object instead of matchoperation?
问题
以下是代码部分的翻译:
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 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("在这里设置查询条件如何操作?");
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);
我需要将查询设置为条件并将其应用于匹配操作。如何实现这一点?
英文:
Below is the code:
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);
The above code works fine. However, I need to make below changes in order to generate criteria dynamically:
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);
I need to set the query to criteria and apply it to the match operation. How to achieve this?
答案1
得分: 1
代替匹配操作,您可以使用以下代码实现:
希望这对您有所帮助。
Criteria criteria = new Criteria();
Query query = new Query();
query.addCriteria(criteria.andOperator(Criteria.where("OwnerId").is(ownerId)));
query.fields().include("SubServices");
query.fields().include("ServerGroupName");
query.fields().exclude("_id");
MongoTemplate.find(query, Map.class, collectionName);
英文:
Instead of Match Operation you can achieve like below code:
Hope, it will helpful
Criteria criteria=new Criteria();
Query query= new Query(); query.addCriteria(criteria.andOperator(Criteria.where("OwnerId").is(ownerId))); query.fields().include("SubServices"); query.fields().include("ServerGroupName"); query.fields().exclude("_id");
MongoTemplate.find(query, Map.Class, collectionName);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论