多个聚合和展开 – Spring数据Mongodb
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了多个聚合和展开 – Spring数据Mongodb,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2647字,纯文字阅读大概需要4分钟。
内容图文
![多个聚合和展开 – Spring数据Mongodb](/upload/InfoBanner/zyjiaocheng/903/3d13aa8ef32a4bcaa9626c34a3d9eb2b.jpg)
我有一个mongo查询,用于展开四个对象数组并根据匹配条件过滤数据.我如何在Spring数据mongodb中执行相同的操作
我曾经使用单一展开,但找不到任何多次展开和匹配操作.
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' } }
])
解决方法:
您可以在1.10.x spring mongo版本中尝试以下聚合.
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<BasicDBObject> results = mongoTemplate.aggregate( aggregation, "generator", BasicDBObject.class).getMappedResults();
生成的Shell查询:
{
"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"}}
]
}
使用静态导入
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"),
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"))
);
内容总结
以上是互联网集市为您收集整理的多个聚合和展开 – Spring数据Mongodb全部内容,希望文章能够帮你解决多个聚合和展开 – Spring数据Mongodb所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。