預覽與解釋

解釋預覽和實際 MongoDB 執行計劃的區別,預覽是檢查 DSL 查詢/管道,而解釋是請求實際的執行計劃。


預覽()

preview() 不會在 MongoDB 上執行查詢,而是將當前 DSL 生成的查詢/管道渲染為診斷用的 Document。這是一個檢查構建結果的 API,而不是實際的執行計劃。

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

• 經典的 findAll、find、count、exists、distinct

• 根聚合 aggregation()

• Atlas Search findAll/find/count/existsQuery

• 向量搜索 findAll/find/count/existsQuery

解釋()

explain() 執行實際的 MongoDB 驅動程序解釋操作,因此需要數據庫連接。

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

可以直接傳遞驅動程序的 QUERY_PLANNER、EXECUTION_STATS、ALL_PLANS_EXECUTIONS 等 ExplainVerbosity 值。

在搜索/向量中也同樣使用

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. 版權所有。介紹隱私政策