跳到主要内容
Couchbase 是一个屡获殊荣的分布式 NoSQL 云数据库,为您的所有云、移动、AI 和边缘计算应用程序提供无与伦比的多功能性、性能、可扩展性和财务价值。 本指南介绍了如何从 Couchbase 数据库加载文档。

安装

npm
npm install @langchain/community @langchain/core couchbase

用法

从 Couchbase 查询文档

有关连接到 Couchbase 集群的更多详细信息,请查看 Node.js SDK 文档 有关使用 SQL++(JSON 的 SQL)查询文档的帮助,请查看文档
import { CouchbaseDocumentLoader } from "@langchain/community/document_loaders/web/couchbase";
import { Cluster } from "couchbase";

const connectionString = "couchbase://"; // valid couchbase connection string
const dbUsername = "Administrator"; // valid database user with read access to the bucket being queried
const dbPassword = "Password"; // password for the database user

// query is a valid SQL++ query
const query = `
    SELECT h.* FROM \`travel-sample\`.inventory.hotel h
    WHERE h.country = 'United States'
    LIMIT 1
`;

连接到 Couchbase 集群

const couchbaseClient = await Cluster.connect(connectionString, {
  username: dbUsername,
  password: dbPassword,
  configProfile: "wanDevelopment",
});

创建加载器

const loader = new CouchbaseDocumentLoader(
  couchbaseClient, // The connected couchbase cluster client
  query // A valid SQL++ query which will return the required data
);

加载文档

您可以通过调用加载器的 load 方法来获取文档。它将返回所有文档的列表。如果您想避免此阻塞调用,可以调用 lazy_load 方法,该方法返回一个迭代器。
// using load method
docs = await loader.load();
console.log(docs);
// using lazy_load
for await (const doc of this.lazyLoad()) {
  console.log(doc);
  break; // break based on required condition
}

使用内容和元数据指定字段

文档内容的一部分字段可以使用 pageContentFields 参数指定。文档的元数据字段可以使用 metadataFields 参数指定。
const loaderWithSelectedFields = new CouchbaseDocumentLoader(
  couchbaseClient,
  query,
  // pageContentFields
  [
    "address",
    "name",
    "city",
    "phone",
    "country",
    "geo",
    "description",
    "reviews",
  ],
  ["id"] // metadataFields
);

const filtered_docs = await loaderWithSelectedFields.load();
console.log(filtered_docs);

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