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

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

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(&quot;OwnerId&quot;).is(ownerId), Criteria.where(&quot;Environment&quot;).is(env), Criteria.where(&quot;ServiceName&quot;).in(api));
			MatchOperation matchOp= Aggregation.match(criteria);
			ProjectionOperation projOp= Aggregation.project(&quot;SubServices&quot;).andInclude(&quot;ServerGroupName&quot;).andInclude(&quot;NodeName&quot;).andInclude(&quot;ServiceVersion&quot;)
					.andExclude(&quot;_id&quot;);
			Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
			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:

            Criteria criteria=new Criteria();
			Query query= new Query();
			if(ownerId!=null &amp;&amp; ownerId&gt;0) {
				query.addCriteria(criteria.andOperator(Criteria.where(&quot;OwnerId&quot;).is(ownerId)));
			}
			if(env!=null) {
				query.addCriteria(criteria.andOperator(Criteria.where(&quot;Environment&quot;).is(env)));
			}
			if(api!=null) {
				query.addCriteria(criteria.andOperator(Criteria.where(&quot;ServiceName&quot;).in(api)));
			}
			MatchOperation matchOp= Aggregation.match(&quot;How to set the query here?&quot;);
			ProjectionOperation projOp= Aggregation.project(&quot;SubServices&quot;).andInclude(&quot;ServerGroupName&quot;).andInclude(&quot;NodeName&quot;).andInclude(&quot;ServiceVersion&quot;)
					.andExclude(&quot;_id&quot;);
			Aggregation aggr = Aggregation.newAggregation(matchOp, projOp);
			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

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

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);

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:

确定