跳到主要内容
MongoDB 是一个 NoSQL 文档型数据库,支持带有动态模式的类 JSON 文档。

概览

MongoDB 文档加载器从 MongoDB 数据库返回 LangChain 文档列表。 加载器需要以下参数:
  • MongoDB 连接字符串
  • MongoDB 数据库名称
  • MongoDB 集合名称
  • (可选)内容过滤器字典
  • (可选)要包含在输出中的字段名称列表
输出采用以下格式
  • pageContent= Mongo 文档
  • metadata={‘database’: ‘[数据库名称]’, ‘collection’: ‘[集合名称]‘}

加载文档加载器

# add this import for running in jupyter notebook
import nest_asyncio

nest_asyncio.apply()
from langchain_community.document_loaders.mongodb import MongodbLoader
loader = MongodbLoader(
    connection_string="mongodb://:27017/",
    db_name="sample_restaurants",
    collection_name="restaurants",
    filter_criteria={"borough": "Bronx", "cuisine": "Bakery"},
    field_names=["name", "address"],
)
docs = loader.load()

len(docs)
71
docs[0]
Document(page_content="Morris Park Bake Shop {'building': '1007', 'coord': [-73.856077, 40.848447], 'street': 'Morris Park Ave', 'zipcode': '10462'}", metadata={'database': 'sample_restaurants', 'collection': 'restaurants'})

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.