英文:
How to use MongoTemplate cond and filter in java code?
问题
如何在springframework_data_mongodb中像处理图片一样使用?
答案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(), "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()));
Reference: org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.
答案2
得分: 0
这是针对您的问题的特定代码,如果您使用 Java 8 和 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(), "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(), "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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论