文字轉查詢的流程

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Apoorva Joshi

Senior AI/ML Developer Advocate, MongoDB

Text-to-query

文字轉查詢示意 1

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Text-to-query

文字轉查詢示意 2

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Text-to-query

文字轉查詢示意 3

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Text-to-query

文字轉查詢示意 4

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Text-to-query

文字轉查詢示意 5

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Text-to-query

文字轉查詢示意 6

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Text-to-query:代理式方法

 

查詢代理示意

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

資料:MongoDB 風格!

 

MongoDB 資料示意

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

結構化查詢語言(SQL)

SQL 查詢範例

SELECT *
FROM table
WHERE year>=1950
LIMIT 5

MongoDB Query API

類 JSON 輸入範例

{ "$and": [
              {"year": {"$gte": 1990}},
              {"year": {"$lt": 2000}}
          ]
}

MongoDB 查詢方法

  • .find(), .insert(), .update(), .delete()
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

範例:movies 集合

_id Title Release Year Cast Genres
573a1396f29313caabce3d17 Larks on a String 1990 [Rudolf Hrusènskè, Vlastimil Brodskè] [Comedy, Drama, Romance]
573a1398f29313caabceab72 The Witching of Ben Wagner 1990 [Sam Bottoms, Harriet Hall, Bettina Rae, Justin Gocke] [Family]
... ... ... [...] [...]
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

單一篩選條件

query = {"genre": "Romance"}
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

單一篩選條件

query = {"genre": "Romance"}
_id Title Release Year Cast Genres
573a1390f29313caabcd6377 Wild and Woolly 1917 [Douglas Fairbanks, Eileen Percy, Calvert Carter, Charles Stevens] [Comedy, Western, Romance]
573a1391f29313caabcd70b4 The Four Horsemen of the Apocalypse 1921 [Pomeroy Cannon, Josef Swickard, Bridgetta Clark, Rudolph Valentino] [Drama, Romance, War]
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多重篩選條件

query = { "$and": [
                    {"year": {"$gte": 1990}},
                    {"year": {"$lt": 2000}}
                  ]
        }
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多重篩選條件

query = { "$and": [
                    {"year": {"$gte": 1990}},
                    {"year": {"$lt": 2000}}
                  ]
        }
_id Title Release Year Cast Genres
573a1396f29313caabce3d17 Larks on a String 1990 [Rudolf Hrusènskè, Vlastimil Brodskè] [Comedy, Drama, Romance]
573a1398f29313caabceab72 The Witching of Ben Wagner 1990 [Sam Bottoms, Harriet Hall, Bettina Rae, Justin Gocke] [Family]
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Aggregation pipelines

聚合管線

db.<collection>.aggregate([
    {stage-1},
    {stage-2},
    {stage-3},
    {stage-4},
])
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多步驟操作

query = [




]
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多步驟操作

query = [

{ "$sort": { "released": -1 } },


]
  • released 排序
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多步驟操作

query = [

{ "$sort": { "released": -1 } },
{ "$limit": 5 },

]
  • released 排序
  • 取前 5 筆結果
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多步驟操作

query = [

{ "$sort": { "released": -1 } },
{ "$limit": 5 },
{ "$project": { "title": 1, "_id": 0 } }
]
  • released 排序
  • 取前 5 筆結果
  • 投影 "title" 欄位
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

多步驟操作

query = [

{ "$sort": { "released": -1 } },
{ "$limit": 5 },
{ "$project": { "title": 1, "_id": 0 } }
]
  • released 排序
  • 取前 5 筆結果
  • 投影 "title" 欄位
Title
The Treasure
Knight of Cups
Sand Castles
Shut In
Dègradè
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

群組聚合

_id Title Release Year Cast Genres
573a1396f29313caabce3d17 Larks on a String 1990 [Rudolf Hrusènskè, Vlastimil Brodskè] [Comedy, Drama, Romance]
573a1398f29313caabceab72 The Witching of Ben Wagner 1990 [Sam Bottoms, Harriet Hall, Bettina Rae, Justin Gocke] [Family]
... ... ... [...] [...]
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

群組聚合

query = [

{ "$unwind": "$genres" },
{ "$group": { "_id": "$genres", "numMovies": { "$sum": 1 } }},
{ "$match": { "numMovies": { "$gte": 50 } } }
]
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

群組聚合

 

Genre Count
Drama 12385
Comedy 6532
Romance 3318
Crime 2457
Thriller 2454
... ...

管線階段

  • $match, $group, $facet, $geoNear, $lookup, $merge, $search, $sort...

運算子

  • 比較:$eq, $gt, $gte, ...
  • 數學:$add, $multiply, $divide...
  • 陣列:$push, $reduce, ...
  • 字串:$toUpper, $toLower, ...
  • 時間:$dateAdd, $dateDiff, ...
使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

一起來練習吧!

使用 MongoDB 與 LangGraph 的 Text-to-Query Agent

Preparing Video For Download...