以下bson是personaddress集合:{ id : 123456, name : foo, address : [{local : yes, location : [{place : {_id:VZG, }, place_lat : 18, place_lan : 83, },{place : {name : ...
以下bson是personaddress集合:
{
"id" : "123456",
"name" : "foo",
"address" : [
{
"local" : "yes",
"location" : [
{
"place" : {
"_id":"VZG",
},
"place_lat" : "18",
"place_lan" : "83",
},
{
"place" : {
"name" : "kerala",
"district" : "palakkad",
"pincode" : "5203689",
},
"place_lat" : "18",
"place_lan" : "83",
}
]
}
]
}
我有另一个地方集合:
{
"_id":"VZG",
"name" : "vizag",
"district" : "Visakhaptanam,
"pincode" : "568923",
}
在mongodb聚合中使用lookup,我想在personaddress集合中嵌入places集合
我试过用
Aggregation aggregation = newAggregation(lookup("places", "address.location.place._id", "_id", "myplaces"), unwind("myplaces"));
AggregationResults<OutputDocument> aggResults = mongoTemplate.aggregate(aggregation, PersonAddressDocument.class, OutputDocument.class);
谁能帮我?
解决方法:
由于您具有嵌套数组,因此在使用$lookup管道之前,需要首先应用$unwind运算符以使嵌入文档非规范化(除非您已在聚合操作中将它们展平):
db.personaddress.aggregate([
{ "$unwind": "$address" },
{ "$unwind": "$address.location" },
{
"$lookup": {
"from": "places",
"localField": "address.location.place._id",
"foreignField": "_id",
"as": "address.location.place",
}
}
])
然后可以实现为(未经测试):
LookupOperation lookupOperation = LookupOperation.newLookup()
.from("places")
.localField("address.location.place._id")
.foreignField("_id")
.as("address.location.place");
Aggregation agg = newAggregation(
unwind("address"),
unwind("address.location"),
lookupOperation
);
AggregationResults<OutputDocument> aggResults = mongoTemplate.aggregate(
agg, PersonAddressDocument.class, OutputDocument.class
);
如果您的Spring Data版本不支持此功能,则解决方法是实现AggregationOperation接口以接收DBObject:
public class CustomGroupOperation implements AggregationOperation {
private DBObject operation;
public CustomGroupOperation (DBObject operation) {
this.operation = operation;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
然后在聚合管道中将$lookup操作实现为DBObject:
DBObject lookupOperation = (DBObject)new BasicDBObject(
"$lookup", new BasicDBObject("from", "places")
.append("localField", "address.location.place._id")
.append("foreignField", "_id")
.append("as", "address.location.place")
);
您可以将其用作:
Aggregation agg = newAggregation(
unwind("address"),
unwind("address.location"),
lookupOperation
);
AggregationResults<OutputDocument> aggResults = mongoTemplate.aggregate(
agg, PersonAddressDocument.class, OutputDocument.class
);
沃梦达教程
本文标题为:java – 在mongodb聚合中查找
基础教程推荐
猜你喜欢
- java实现在原有日期时间上加几个月或几天 2023-06-23
- java – Hibernate – 种子数据库,不使用import.sql 2023-11-04
- Restful Webservices使用Java,Apache Axis2,Hibernate和MySQL及其性能 2023-11-06
- Springcloud Config配置中心使用与相关介绍 2023-05-18
- WebService的用户控制方式与加密算法分类的整理 2023-07-31
- SpringBoot自动配置深入探究实现原理 2023-04-13
- Spring深入刨析声明式事务注解的源码 2023-03-06
- Java作为与MySQL交互的cron脚本与使用PHP 2023-11-04
- 深入了解Java设计模式之职责链模式 2023-05-14
- Intellij IDEA如何修改配置文件位置 2023-04-17