如何在Java代码中使用MongoTemplate的条件(cond)和筛选(filter)功能?

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

How to use MongoTemplate cond and filter in java code?

问题

如何在springframework_data_mongodb中像处理图片一样使用?

图片描述

英文:

How to use in springframework_data_mongodb like image?

enter image description here

答案1

得分: 0

这是来自MongoDB手册的一个示例,演示了如何将$filter聚合操作转换为Spring Data MongoDB代码。相同的语法可以用于与“$filter”一起使用条件的问题。

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "test");
Aggregation agg = newAggregation(
    project()
        .and(filter("items")
            .as("item")
            .by(Gte.valueOf("item.price").greaterThanEqualToValue(100)))
        .as("items")
);

AggregationResults<Document> results = mongoOps.aggregate(agg, "sales", Document.class);
results.forEach(doc -> System.out.println(doc.toJson()));

参考链接: org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter

英文:

This is an example from MongoDB manual for $filter aggregation converted to Spring Data MongoDB code. The same syntax can be applied to the question about using the $filter along with a condition.

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), &quot;test&quot;);
Aggregation agg = newAggregation(
                      project()
                         .and(filter(&quot;items&quot;)
                              .as(&quot;item&quot;)
                              .by(Gte.valueOf(&quot;item.price&quot;).greaterThanEqualToValue(100)))
                         .as(&quot;items&quot;)
);
		
AggregationResults&lt;Document&gt; results = mongoOps.aggregate(agg, &quot;sales&quot;, Document.class);		
results.forEach(doc -&gt; System.out.println(doc.toJson()));

Reference: org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.

答案2

得分: 0

这是针对您的问题的特定代码,如果您使用 Java 8Spring Data MongoDB 3.0.3.RELEASE

import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
import static org.springframework.data.mongodb.core.query.Criteria.where;

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "database");
ProjectionOperation projectionOperation =  project().and(filter("fields")
                     .as("field")
                     .by(Eq.valueOf("field.code").equalToValue("EngName")))
                     .as("fields");
MatchOperation matchOperation = match(where("fields.value").in("A","B"));
Aggregation agg = Aggregation.newAggregation(projectionOperation, matchOperation);
AggregationResults<Document> docs = mongoOps.aggregate(agg, "collection", Document.class);
docs.forEach(doc->System.out.println(doc));
英文:

Here is the code specific to your problem if you use Java 8 and Spring Data MongoDB 3.0.3.RELEASE.

import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
import static org.springframework.data.mongodb.core.query.Criteria.where;

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), &quot;database&quot;);
ProjectionOperation projectionOperation =  project().and(filter(&quot;fields&quot;)
	             .as(&quot;field&quot;)
	             .by(Eq.valueOf(&quot;field.code&quot;).equalToValue(&quot;EngName&quot;)))
	             .as(&quot;fields&quot;);
MatchOperation matchOperation = match(where(&quot;fields.value&quot;).in(&quot;A&quot;,&quot;B&quot;));
Aggregation agg = Aggregation.newAggregation(projectionOperation,matchOperation);
AggregationResults&lt;Document&gt; docs = mongoOps.aggregate(agg, &quot;collection&quot;, Document.class);
		
docs.forEach(doc-&gt;System.out.println(doc));

huangapple
  • 本文由 发表于 2020年9月22日 18:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64007682.html
匿名

发表评论

匿名网友

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

确定