$lookup

simple joinとlet/pipelineベースのLookupSpec、Driver-native raw stage、unwindとlookup countのフローを構成します。


localField / foreignField

java
LookupSpec spec = LookupSpec.builder()
    .as("orders")
    .localField("id")
    .foreignField("userId")
    .build();

let + pipeline + $expr

java
LookupSpec spec = LookupSpec.builder()
    .as("orders")
    .bindConditionFields("id", eq, "userId")
    .bindConditionConst("DONE", eq, "status")
    .limit(10)
    .sorts(sort -> sort.desc("createdAt"))
    .build();

• bindConditionFields(leftField, condition, rightField)

• bindConditionFieldsLeftToObjectId(...)

• bindConditionConst, bindConditionBetween, bindConditionLike

• bindConditionExists, bindConditionIsNull, bindConditionIsNotNull

• Driver-native lookup pipeline stagesのためのrawStage(Bson)

• unwind(preserveNullAndEmptyArrays)

• outerStage(Bson) / outerStages(...)は$lookupの後のステージ用です

Lookupを実行

java
var left = dsl
    .executeEntity(User.class, MongoTemplateName.FRONT)
    .fields(pair("status", "ACTIVE"))
    .end()
    .findAll()
    .paging(0, 20);

var right = dsl
    .executeEntity(Order.class, MongoTemplateName.FRONT)
    .fields(pair("status", "DONE"))
    .end()
    .findAll();

Flux<ResultTuple<User, List<Order>>> joined =
    left.executeLookup(right, spec);

unwind(...)를 사용하지 않은 기본 findAll lookup은 right 결과를 List<R>로 반환합니다. 단건 left/right가 필요하면 find().executeLookup(...) 경로를 사용합니다.

Lookup + count / exists

java
Mono<PageResult<ResultTuple<User, List<Order>>>> page =
    left.executeLookupAndCount(right, spec);

Mono<ResultTuple<Long, Long>> counts =
    leftCount.executeLookup(rightCount, spec);

executeLookupAndCount(...)は$facet(data, count)形式でdataとtotalCountを返します。count/existsビルダーもlookup結果を左/右のタプルとして受け取ることができます。

© 2026 Byeolnaerim. 無断転載を禁じます。紹介プライバシーポリシー