Preview & Explain

อธิบายความแตกต่างระหว่าง preview ที่ตรวจสอบ DSL query/pipeline ก่อนการดำเนินการและ explain ที่ขอแผนการดำเนินการจริงของ MongoDB


preview()

preview() จะไม่ดำเนินการ query กับ MongoDB แต่จะเรนเดอร์ query/pipeline ที่สร้างโดย 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();

• classic findAll, find, count, exists, distinct

• root aggregation()

• Atlas Search findAll/find/count/existsQuery

• Vector Search findAll/find/count/existsQuery

explain()

explain() จะดำเนินการอธิบายจริงของ MongoDB Driver ดังนั้นจึงต้องการการเชื่อมต่อฐานข้อมูล

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

สามารถส่งค่าของ ExplainVerbosity เช่น QUERY_PLANNER, EXECUTION_STATS, ALL_PLANS_EXECUTIONS ของ Driver ได้โดยตรง

ใช้ได้เช่นเดียวกันใน 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();