【Java基础复习】Aggregation

Pipelines and Steams

A pipeline is a sequence of aggregate operations, containing the following components: a source; zero or more intermediate operations and a terminal operation.
在这里插入图片描述
Aggregate Operation区别于Iterators的地方:

  • They use internal iteration: Aggregate operations do not contain a method like next to instruct them to process the next element of the collection. With internal delegation, your application determines what collection it iterates, but the JDK determines how to iterate the collection. With external iteration, your application determines both what collection it iterates and how it iterates it. However, external iteration can only iterate over the elements of a collection sequentially. In
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用Aggregation和lookup进行多字段匹配连表操作。Aggregation是MongoDB中的一种数据处理方法,可以对数据集合进行分组、筛选、计算等操作。而lookup则是Aggregation中的一个操作符,可以实现类似于SQL中的join操作,将两个集合进行关联查询。 具体步骤如下: 1. 使用Aggregation操作对主表进行筛选和分组,以及进行关联查询所需的字段。 2. 使用lookup操作符将主表和从表进行关联查询,指定主表和从表的关联字段,并指定返回结果的字段。 3. 将查询结果进行处理,得到最终的连表结果。 示例代码如下: ``` MongoCollection<Document> mainColl = db.getCollection("main"); // 定义Aggregation操作 AggregateIterable<Document> agg = mainColl.aggregate(Arrays.asList( Aggregates.match(Filters.eq("type", "A")), // 筛选符合条件的数据 Aggregates.group("$id", Accumulators.first("name", "$name")), // 对符合条件的数据进行分组 Aggregates.lookup("slave", "_id", "main_id", "slave") // 进行关联查询 )); // 处理查询结果 List<Document> resultList = new ArrayList<>(); agg.forEach((Block<Document>) resultList::add); ``` 在上述代码中,我们定义了一个Aggregation操作,首先使用`match`操作筛选出符合条件的数据,然后使用`group`操作进行分组,并将分组后的结果中的`id`和`name`字段作为返回结果的一部分。最后,使用`lookup`操作将主表和从表进行关联查询,指定主表和从表的关联字段分别为`_id`和`main_id`,并指定返回结果的字段为`slave`。 执行完Aggregation操作后,我们可以得到一个查询结果的迭代器,通过遍历该迭代器,将结果添加到一个结果列表中,最终得到最终的连表结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值