Multiple aggregation and expansion – Spring Data MongoDB

I have a mongo query to expand four object arrays and filter data based on matching conditions. How do I perform the same operation in Spring Data mongodb

< p>I used to use a single expansion, but I couldn’t find any multiple expansion and matching operations.

db.generator.aggregate([
{ $unwind :' $eCList'},
{ $unwind :'$pcList'},
{ $unwind :'$cityList'},
{ $unwind :'$eATypeList'},
{ $match: {'eCList.eCCode': {$eq:'enccode1'} }},
{ $match: {'pcList.pcCode': {$eq:'pccode1'} }},< br />{ $match: {'cityList.cityCode': {$eq:'citycode1'} }},
{ $match: {'eATypeList.eATypeCode': {$eq:'eATypeCode1'} }} ,
{ $project: {generatorData:'$generatorData',pcList:'$pcList',
generatorCode:'$generatorCode', eCId:'$eCList.eCId',
eCCode: '$eCList.eCCode', eCKey:'$eCList.eCKey'} }
])

You can try the following aggregation in the 1.10.x spring mongo version.

Aggregation aggregation = Aggregation.newAggregation(
Aggregation.unwind(" eCList"),
Aggregation.unwin d("pcList"),
Aggregation.unwind("cityList"),
Aggregation.unwind("eATypeList"),
Aggregation.match(Criteria.where("eCList.eCCode" ).is("enccode1")),
Aggregation.match(Criteria.where("pcList.pcCode").is("pccode1")),
Aggregation.match(Criteria.where(" cityList.cityCode").is("citycode1")),
Aggregation.match(Criteria.where("eATypeList.eATypeCode").is("eATypeCode1")),
Aggregation.project(" generatorData", "pcList", "generatorCode").
andInclude(Aggregation.bind("eCId", "eCList.eCId")).
andInclude(Aggregation.bind("eCCode", "eCList .eCCode")).
andInclude(Aggregation.bind("eCKey", "eCList.eCKey"))
);
List results = mongoTemplate.aggregate( aggregation, " generator", BasicDBObject.class).getMappedResults();

Shell query generated:

{
"aggregate":"__collection__" ,
"pipeline":[
{"$unwind":"$eCList"},
{" $unwind":"$pcList"},
{"$unwind":"$cityList"},
{"$unwind":"$eATypeList"},
{"$match ":{"eCList.eCCode":"enccode1"}},
{"$match":{"pcList.pcCode":"pccode1"}},
{"$match":{" cityList.cityCode":"citycode1"}},
{"$match":{"eATypeList.eATypeCode":"eATypeCode1"}},
{"$project":{
" generatorData":1,
"pcList":1,
"generatorCode":1,
"eCId":"$eCList.eCId",
"eCCode":"$ eCList.eCCode",
"eCKey":"$eCList.eCKey"}}
]
}

Use static import

import org.springframework.data.mongodb.core.query.Criteria;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

Aggregation aggregation = newAggregation(
unwind("eCList"),
unwind("pcList"),
unwind("cityList"),
unwind("eATypeList"),< br /> match(where("eCList.eCCode").is("enccode1")),
match(where("pcList.pcCode").is("pccode1")),< br /> match(where("cityList.cityCode").is("citycode1")),
match(where("eATypeList.eATypeCode").is("eATypeCode1")),
project ("generatorData", "pcList", "generatorCode").
andInclude(bind("eCId", "eCList.eCId")).
andInclude(bind("eCCode", "eCList.eCCode ")).
andInclude(bind("eCKey", "eCList.eCKey"))
);

I have a mongo query, Used to expand four object arrays and filter data based on matching conditions. How can I perform the same operation in Spring Data mongodb

I used to use single expansion, but I couldn’t find any multiple expansions and Match operation.

db.generator.aggregate([
{ $unwind :'$eCList'},
{ $unwind :'$pcList' },
{ $unwind :'$cityList'},
{ $unwind :'$eATypeList'},
{ $match: {'eCList.eCCode': {$eq: ' enccode1'} }},
{ $match: {'pcList.pcCode': {$eq:'pccode1'} }},
{ $match: {'cityList.cityCode': {$eq :'citycode1'} }},
{ $match: {'eATypeList.eATypeCode': {$eq:'eATypeCode1'} }},
{ $project: {generatorData:'$generatorData', pcList:'$pcList',
generatorCode:'$generatorCode' , eCId:'$eCList.eCId',
eCCode:'$eCList.eCCode', eCKey:'$eCList.eCKey'} }
])

You can try the following aggregation in the 1.10.x spring mongo version.

Aggregation aggregation = Aggregation.newAggregation(
Aggregation .unwind("eCList"),
Aggregation.unwind("pcList"),
Aggregation.unwind("cityList"),
Aggregation.unwind("eATypeList"),
Aggregation.match(Criteria.where("eCList.eCCode").is("enccode1")),
Aggregation.match(Criteria.where("pcList.pcCode").is("pccode1")) ,
Aggregation.match(Criteria.where("cityList.cityCode").is("citycode1")),
Aggregation.match(Criteria.where("eATypeList.eATypeCode").is(" eATypeCode1")),
Aggregation.project("generatorData", "pcList", "generatorCode").
andInclude(Aggregation.bind("eCId", "eCList.eCId")).
andInclude(Aggregation.bind("eCCode", "eCList.eCCode")).
andInclude(Aggregation.bind("eCKey", "eCList.eCKey"))
);
List results = mongoTemplate.aggregate( aggregation, "generator", BasicDBObject.class).getMappedResults();

Generate Shell query:

{
"aggregate":"__collection__",
"pipeline":[
{"$unwind": "$eCList"},
{"$unwind":"$pcList"},
{"$unwind":"$cityList"},
{"$unwind":"$ eATypeList"},
{"$match":{"eCList.eCCode":"enccode1"}},
{"$match":{"pcList.pcCode":"pccode1"}},
{"$match":{"cityList.cityCode":"citycode1"}},
{"$match":{"eATypeList.eATypeCode":"eATypeCode1"}},
{"$project":{
"generatorData":1,
"pcList":1,
"generatorCode":1,
"eCId":"$eCList.eCId ",
"eCCode":"$eCList.eCCode",
"eCKey":"$eCList.eCKey"}}
]
}

Use static import

import org.springframework.data.mongodb.core.query.Criteria;
import static org.springframework.data.mongodb.core.aggregation. Aggreg ation.*;

Aggregation aggregation = newAggregation(
unwind("eCList"),
unwind("pcList"),
unwind("cityList"),
unwind("eATypeList"),
match(where("eCList.eCCode").is("enccode1")),
match(where("pcList.pcCode").is ("pccode1")),
match(where("cityList.cityCode").is("citycode1")),
match(where("eATypeList.eATypeCode").is("eATypeCode1" )),
project("generatorData", "pcList", "generatorCode").
andInclude(bind("eCId", "eCList.eCId")).
andInclude(bind( "eCCode", "eCList.eCCode")).
andInclude(bind("eCKey", "eCList.eCKey"))
);

Leave a Comment

Your email address will not be published.