在mongodb聚合中查找

编程入门 行业动态 更新时间:2024-10-26 02:31:31
本文介绍了在mongodb聚合中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下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聚合中使用查找,我想在 personaddress <中嵌入 places 集合/ code>集合

Using lookup in mongodb aggregation, I want to embed places collection in personaddress collection

我尝试使用

Aggregation aggregation = newAggregation(lookup("places", "address.location.place._id", "_id", "myplaces"), unwind("myplaces")); AggregationResults<OutputDocument> aggResults = mongoTemplate.aggregate(aggregation, PersonAddressDocument.class, OutputDocument.class);

任何人都可以帮助我吗?

Can anyone help me?

推荐答案

由于您有嵌套数组,因此需要应用 $ unwind 运营商首先,为了在使用 $ lookup 管道(除非您已在汇总操作中将其展平):

Since you have nested arrays, you need to apply the $unwind operator first in order to denormalise the embedded documents before using the $lookup pipeline (unless you have already flattened them in your aggregation operation):

db.personaddress.aggregate([ { "$unwind": "$address" }, { "$unwind": "$address.location" }, { "$lookup": { "from": "places", "localField": "address.location.place._id", "foreignField": "_id", "as": "address.location.place", } } ])

然后可以实现为(未经测试):

which can then be implemented as (untested):

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操作:

Then implement the $lookup operation as a DBObject in the aggregation pipeline:

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 );

更多推荐

在mongodb聚合中查找

本文发布于:2023-11-22 13:01:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!