文本到查询工作流

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

Apoorva Joshi

Senior AI/ML Developer Advocate, MongoDB

文本到查询

文本到查询1.jpg

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

文本到查询

文本到查询2.jpg

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

文本到查询

文本到查询3.jpg

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

文本到查询

文本到查询4.jpg

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

文本到查询

文本到查询5.jpg

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

文本到查询

文本到查询6.jpg

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

文本到查询:Agent 方法

 

query_agent.png

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

数据:MongoDB 风格!

 

mongodb数据.png

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

结构化查询语言(SQL)

SQL 查询示例

SELECT *
FROM table
WHERE year>=1950
LIMIT 5

MongoDB 查询 API

类 JSON 输入示例

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

MongoDB 查询方法

  • .find(), .insert(), .update(), .delete()
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

示例:movies 集合

_id 标题 上映年 演员 类型
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 代理

单一过滤条件

query = {"genre": "Romance"}
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

单一过滤条件

query = {"genre": "Romance"}
_id 标题 上映年 演员 类型
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 代理

多重过滤条件

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

多重过滤条件

query = { "$and": [
                    {"year": {"$gte": 1990}},
                    {"year": {"$lt": 2000}}
                  ]
        }
_id 标题 上映年 演员 类型
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 代理

聚合管道

管道.png

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

多步操作

query = [




]
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

多步操作

query = [

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


]
  • released 排序
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

多步操作

query = [

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

]
  • released 排序
  • 限制为 5
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

多步操作

query = [

{ "$sort": { "released": -1 } },
{ "$limit": 5 },
{ "$project": { "title": 1, "_id": 0 } }
]
  • released 排序
  • 限制为 5
  • 投影 "title"
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

多步操作

query = [

{ "$sort": { "released": -1 } },
{ "$limit": 5 },
{ "$project": { "title": 1, "_id": 0 } }
]
  • released 排序
  • 限制为 5
  • 投影 "title"
标题
The Treasure
Knight of Cups
Sand Castles
Shut In
Dègradè
使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

分组聚合

_id 标题 上映年 演员 类型
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 代理

分组聚合

query = [

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

分组聚合

 

类型 数量
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 代理

Vamos praticar!

使用 MongoDB 和 LangGraph 的 Text-to-Query 代理

Preparing Video For Download...