0

I have a Mongo query that should be converted to Java Spring-boot code,

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

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.

Azim Uddin
  • 19
  • 1
  • 5

1 Answers1

0

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. and if you don't know how to configure mongo template then here it is

MongoTemplate mongoTemplate= new MongoTemplate(mongoClientObject, dbName);
Azim Uddin
  • 19
  • 1
  • 5