How to perform aggregation in mongodb using Java by setting query object instead of matchoperation?

huangapple go评论102阅读模式
英文:

How to perform aggregation in mongodb using Java by setting query object instead of matchoperation?

问题

以下是代码部分的翻译:

  1. Criteria criteria=null;
  2. criteria = new Criteria().andOperator(Criteria.where("OwnerId").is(ownerId), Criteria.where("Environment").is(env), Criteria.where("ServiceName").in(api));
  3. MatchOperation matchOp= Aggregation.match(criteria);
  4. ProjectionOperation projOp= Aggregation.project("SubServices").andInclude("ServerGroupName").andInclude("NodeName").andInclude("ServiceVersion")
  5. .andExclude("_id");
  6. Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
  7. AggregationResults<Document> aggregate = mongoOperations.aggregate(aggr, "CNF_SERVICE",Document.class,"config",env);

上述代码运行良好。但是,为了动态生成条件,我需要进行以下更改:

  1. Criteria criteria=new Criteria();
  2. Query query= new Query();
  3. if(ownerId!=null && ownerId>0) {
  4. query.addCriteria(criteria.andOperator(Criteria.where("OwnerId").is(ownerId)));
  5. }
  6. if(env!=null) {
  7. query.addCriteria(criteria.andOperator(Criteria.where("Environment").is(env)));
  8. }
  9. if(api!=null) {
  10. query.addCriteria(criteria.andOperator(Criteria.where("ServiceName").in(api)));
  11. }
  12. MatchOperation matchOp= Aggregation.match("在这里设置查询条件如何操作?");
  13. ProjectionOperation projOp= Aggregation.project("SubServices").andInclude("ServerGroupName").andInclude("NodeName").andInclude("ServiceVersion")
  14. .andExclude("_id");
  15. Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
  16. AggregationResults<Document> aggregate = mongoOperations.aggregate(aggr, "CNF_SERVICE",Document.class,"config",env);

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

英文:

Below is the code:

  1. Criteria criteria=null;
  2. criteria = new Criteria().andOperator(Criteria.where(&quot;OwnerId&quot;).is(ownerId), Criteria.where(&quot;Environment&quot;).is(env), Criteria.where(&quot;ServiceName&quot;).in(api));
  3. MatchOperation matchOp= Aggregation.match(criteria);
  4. ProjectionOperation projOp= Aggregation.project(&quot;SubServices&quot;).andInclude(&quot;ServerGroupName&quot;).andInclude(&quot;NodeName&quot;).andInclude(&quot;ServiceVersion&quot;)
  5. .andExclude(&quot;_id&quot;);
  6. Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
  7. AggregationResults&lt;Document&gt; aggregate = mongoOperations.aggregate(aggr, &quot;CNF_SERVICE&quot;,Document.class,&quot;config&quot;,env);

The above code works fine. However, I need to make below changes in order to generate criteria dynamically:

  1. Criteria criteria=new Criteria();
  2. Query query= new Query();
  3. if(ownerId!=null &amp;&amp; ownerId&gt;0) {
  4. query.addCriteria(criteria.andOperator(Criteria.where(&quot;OwnerId&quot;).is(ownerId)));
  5. }
  6. if(env!=null) {
  7. query.addCriteria(criteria.andOperator(Criteria.where(&quot;Environment&quot;).is(env)));
  8. }
  9. if(api!=null) {
  10. query.addCriteria(criteria.andOperator(Criteria.where(&quot;ServiceName&quot;).in(api)));
  11. }
  12. MatchOperation matchOp= Aggregation.match(&quot;How to set the query here?&quot;);
  13. ProjectionOperation projOp= Aggregation.project(&quot;SubServices&quot;).andInclude(&quot;ServerGroupName&quot;).andInclude(&quot;NodeName&quot;).andInclude(&quot;ServiceVersion&quot;)
  14. .andExclude(&quot;_id&quot;);
  15. Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
  16. AggregationResults&lt;Document&gt; aggregate = mongoOperations.aggregate(aggr, &quot;CNF_SERVICE&quot;,Document.class,&quot;config&quot;,env);

I need to set the query to criteria and apply it to the match operation. How to achieve this?

答案1

得分: 1

代替匹配操作,您可以使用以下代码实现:
希望这对您有所帮助。

  1. Criteria criteria = new Criteria();
  2. Query query = new Query();
  3. query.addCriteria(criteria.andOperator(Criteria.where("OwnerId").is(ownerId)));
  4. query.fields().include("SubServices");
  5. query.fields().include("ServerGroupName");
  6. query.fields().exclude("_id");
  7. 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);

huangapple
  • 本文由 发表于 2020年9月7日 20:41:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63777851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定