Preview & Explain

Explains the difference between previewing the DSL query/pipeline before execution and requesting the actual MongoDB execution plan with explain.


preview()

preview() renders the current DSL into a diagnostic Document without executing the query on MongoDB. It is an API to check the build result, not the actual execution plan.

java
Mono<Document> preview = dsl
    .executeEntity(User.class, MongoTemplateName.FRONT)
    .fields(pair("status", "ACTIVE"))
    .end()
    .findAll()
    .paging(0, 20)
    .sorts(sort -> sort.desc("id"))
    .preview();

• classic findAll, find, count, exists, distinct

• root aggregation()

• Atlas Search findAll/find/count/existsQuery

• Vector Search findAll/find/count/existsQuery

explain()

explain() executes the actual MongoDB Driver explain operation, so a database connection is required.

java
Mono<Document> explain = dsl
    .executeEntity(User.class, MongoTemplateName.FRONT)
    .fields(pair("status", "ACTIVE"))
    .end()
    .findAll()
    .explain(ExplainVerbosity.EXECUTION_STATS);

You can pass ExplainVerbosity values like QUERY_PLANNER, EXECUTION_STATS, ALL_PLANS_EXECUTIONS directly from the Driver.

Used similarly in Search / Vector.

java
Mono<Document> searchPreview = dsl
    .executeEntity(Article.class, key)
    .search("articles_default")
    .text(text -> text.path("title").query("mongodb"))
    .findAll()
    .preview();

Mono<Document> vectorExplain = dsl
    .executeEntity(Article.class, key)
    .vectorSearch("articles_vector_index")
    .path("embedding")
    .queryVector(embedding)
    .limit(20)
    .approximate(200)
    .findAll()
    .explain();
© 2026 Byeolnaerim. All rights reserved.IntroductionPrivacy Policy