[英]Java Spring-boot code for aggregate operation with projection and filter in MongoDB
I have a Mongo query that should be converted to Java Spring-boot code,我有一个应该转换为 Java Spring-boot 代码的 Mongo 查询,
Query询问
db.orgConfigData.aggregate([
{"$match" : { "organizationId": 339975}},
{$project: {
domains:{
$filter: {
input: "$domains",
as : "domains",
cond: {$eq: ["$$domains.activeInd", true]}
}
}}
}])
I have tried using the below code for the beginning but got stuck on how to add a filter in the projection in java spring我一开始就尝试使用下面的代码,但在如何在 java spring 的投影中添加过滤器时遇到了困难
MatchOperation matchStage = Aggregation.match(new Criteria("organizationId").is(orgId));
ProjectionOperation projectStage = Aggregation.project(?);
can someone help me with the code for the above query?有人可以帮我提供上述查询的代码吗? Thanks in advance.
提前致谢。
I have found a solution for this.我已经找到了解决方案。 Here is the solution to this.
这是解决此问题的方法。
Here are the imports.这里是进口。
import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter;
import org.springframework.data.mongodb.core.query.Criteria;
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(new Criteria("organizationId").is(orgId)),
Aggregation.project("domains")
.and(Filter.filter("domains").as("domains").by(valueOf("domains.activeInd").equalToValue(true)))
.as("domains")
);
AggregationResults<outputclass> resultObject = mongoTemplate.aggregate(aggregation, "collection_name",
outputclass.class);
List<outputclass> resultList=resultObject.getMappedResults();
This works only with mongoTemplate.这仅适用于 mongoTemplate。 and if you don't know how to configure mongo template then here it is
如果您不知道如何配置 mongo 模板,那么这里就是
MongoTemplate mongoTemplate= new MongoTemplate(mongoClientObject, dbName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.