栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring数据mongo中的Mongodb $ Lookup

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring数据mongo中的Mongodb $ Lookup

并非每个“新”功能都会使其立即进入诸如spring-mongo之类的抽象层。

因此,您需要做的就是定义一个使用该

AggregationOperation
接口的类,该类将使用直接指定的BSON对象作为其内容:

public class CustomAggregationOperation implements AggregationOperation {    private DBObject operation;    public CustomAggregationOperation (DBObject operation) {        this.operation = operation;    }    @Override    public DBObject toDBObject(AggregationOperationContext context) {        return context.getMappedObject(operation);    }}

然后,您可以像这样在聚合中使用:

Aggregation aggregation = newAggregation(    match(        Criteria.where("username").is("user001")    ),    new CustomAggregationOperation(        new BasicDBObject( "$lookup", new BasicDBObject("from", "NewFeedContent")     .append("localField","content.contentId")     .append("foreignField", "_id")     .append("as", "NewFeedContent")        )    ))

该图显示了与内置

match()
管道帮助程序混合的自定义类。

在每个帮助器下发生的所有事情都是将它们序列化为BSON表示形式,例如

DBObject
无论如何。因此,这里的构造函数只是直接获取对象,并直接从将该对象返回
.toDBObject()
,这是在序列化pipline内容时将被调用的接口上的标准方法。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483528.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号