对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

用于查询优化的索引

索引是 MongoDB 中为高效查询提供支持的数据结构。它们包含文档中部分数据的副本,以提高查询效率。

如果没有索引,MongoDB 必须扫描集合中的每份文档,从而找到与每个查询相匹配的文档。这些集合扫描很慢,可能会对应用程序的性能产生负面影响。使用索引来限制 MongoDB 扫描的文档数量,可以提高查询效率,促进返回速度。

当您对 MongoDB 执行查询时,您的查询可以包括以下三个部分:

  • 用于指定您要查找的一个或多个字段和值的查询条件

  • 影响查询执行的选项,例如读关注

  • 用于指定您希望 MongoDB 返回的字段的投影条件(可选)

当查询条件和查询的投影中指定的所有字段都已经被索引,MongoDB 会直接从索引返回结果,而无需扫描集合中的任何文档,也不需要将这些文档加载到内存中。

有关如何确保索引涵盖查询条件和投影的更多信息,请参阅 MongoDB 手册中关于查询覆盖范围和索引交集的文章。

若要提高查询性能,请对应用程序查询中经常出现的字段以及其他操作返回的排序结果中经常出现的字段建立索引。您添加的每个索引在活动状态下都会占用磁盘空间和内存,因此您可能需要跟踪索引内存和磁盘使用情况以进行容量规划。此外,当写入操作更新索引字段时,MongoDB 还会更新相关索引。

有关设计数据模型和选择适合您应用程序的索引的更多信息,请参阅 MongoDB 服务器文档中的索引策略和数据建模和索引部分。

您可以使用 listIndexes() 方法列出集合的所有索引。listIndexes() 方法采用可选的 ListIndexesOptions 参数。listIndexes() 方法返回 ListIndexesCursor 类型的对象。

以下代码使用 listIndexes() 方法列出集合中的所有索引:

// List the indexes on the collection and output them as an arrayconst result = await collection.listIndexes().toArray(); // Print the list of indexesconsole.log("Existing indexes:\n");for(const doc in result){ console.log(doc);}

MongoDB 支持多种索引类型来进行数据查询。以下部分描述了最常见的索引类型,并提供了创建每种索引类型的示例代码。

单字段索引是指可提高特定查询的性能的索引,而此类查询会对文档的单个字段指定升序或降序排序顺序。

以下示例使用 createIndex() 方法对 sample_mflix 数据库的 movies 集合的 title 字段创建升序索引。

const database = client.db("sample_mflix");const movies = database.collection("movies"); // Create an ascending index on the "title" field in the// "movies" collection.const result = await movies.createIndex({ title: 1 });console.log(`Index created: ${result}`);

以下是一个使用上述所建索引实现高效查询的示例。

// Define the query parametersconst query = { title: "Batman" }const sort = { title: 1 };const projection = { _id: 0, title: 1 };// Execute the query using the defined parametersconst cursor = movies .find(query) .sort(sort) .project(projection); for await (const doc of cursor) { console.log(doc);}

如要了解更多信息,请参阅单字段索引。

复合索引可提高为文档的多个字段指定升序或降序排序顺序的查询的性能。您必须为索引中的每个字段指定方向(升序或降序)。

以下示例使用 createIndex() 方法在 sample_mflix 数据库的 movies 集合中的 type 和 genre 字段上创建复合索引。

// Connect to the "sample_mflix" databaseconst database = client.db("sample_mflix");// Access the database's "movies" collectionconst movies = database.collection("movies"); // Create an ascending index on the "type" and "genre" fields// in the "movies" collection.const result = await movies.createIndex({ type: 1, genre: 1 });console.log(`Index created: ${result}`);

以下是一个使用上述所建索引实现高效查询的示例。

// Define a query to find movies in the "Drama" genreconst query = { type: "movie", genre: "Drama" };// Define sorting criteria for the query resultsconst sort = { type: 1, genre: 1 };// Include only the type and genre fields in the query resultsconst projection = { _id: 0, type: 1, genre: 1 }; // Execute the query using the defined criteria and projectionconst cursor = movies .find(query) .sort(sort) .project(projection); for await (const doc of cursor) { console.log(doc);}

要了解更多信息,请参阅复合索引。

多键索引可提高对包含数组值的字段的查询性能。

您可以通过调用 createIndex() 方法,对具有数组值的字段创建多键索引。以下代码将对 sample_mflix 数据库 movies 集合中的 cast 字段创建升序索引:

const database = client.db("sample_mflix");const movies = database.collection("movies"); // Create a multikey index on the "cast" field in the "movies" collectionconst result = await movies.createIndex({ cast: 1 });

以下代码会查询多键索引以查找 cast 字段值包含 "Viola Davis" 的文档:

const query = { cast: "Viola Davis" };const projection = { _id: 0, cast: 1 , title: 1 }; // Perform a find operation with the preceding filter and projectionconst cursor = movies .find(query) .project(projection); for await (const doc of cursor) { console.log(doc);}

在查询覆盖、索引绑定计算和排序行为方面,多键索引的行为与非多键索引不同。有关多键索引的完整解释,包括对其行为和限制的讨论,请参阅 MongoDB Server 手册中的多键索引页面。

聚集索引是指可提高针对集群化集合的插入、更新和删除操作的性能的索引。集群化集合可存储按聚集索引键值排序的文档。

要创建聚集索引,请在 CollectionOption 中指定 clusteredIndex 选项。clusteredIndex 选项必须将 _id 字段指定为键,并将唯一字段指定为 true。

以下示例使用 createCollection() 方法在 tea 数据库的 vendors 集合中的 _id 字段上创建聚集索引。

const db = client.db('tea');await db.createCollection('ratings', { clusteredIndex: { key: { _id: 1 }, unique: true }});

如需了解更多信息,请参阅聚集索引和聚集集合。

文本索引支持对字符串内容进行文本查询。这些索引可以包括值为字符串或字符串元素数组的任何字段。

MongoDB支持各种语言的文本查询,因此可以在创建索引时指定默认语言作为选项。您还可以指定权重选项,对索引中某些文本字段进行优先排序。这些权重表示字段相对于其他索引字段的重要性。

要学习;了解有关文本查询的更多信息,请参阅我们的文本查询指南。

以下示例使用createIndex() 方法执行以下操作:

  • 在 blogPosts 集合中的 title 和 body 字段上创建一个 text 索引

  • 将 english 指定为默认语言

  • 将 body 的字段权重设为 10,并将 title 的字段权重设为 3

// Get the database and collection on which to create the index const myDB = client.db("testDB");const myColl = myDB.collection("blogPosts"); // Create a text index on the "title" and "body" fieldsconst result = await myColl.createIndex( { title: "text", body: "text" }, { default_language: "english", weights: { body: 10, title: 3 } });

以下查询使用了在上述代码中创建的文本索引:

// Query for documents where body or title contain "life ahead"const query = { $text: { $search: "life ahead" } }; // Show only the title fieldconst projection = { _id: 0, title: 1 }; // Execute the find operationconst cursor = myColl.find(query).project(projection); for await (const doc of cursor) { console.log(doc);}

要了解有关文本索引的更多信息,请参阅服务器手册中的文本索引。

MongoDB 支持使用 2dsphere 索引查询地理空间坐标数据。借助 2dsphere 索引,您可以针对地理空间数据进行包含、相交和邻近范围方面的查询。有关使用 MongoDB Node.js 驱动程序查询地理空间数据的更多信息,请阅读搜索地理空间指南。

要创建 2dsphere 索引,您必须指定仅包含 GeoJSON 对象的字段。有关该类型的详细信息,请参阅 MongoDB 服务器手册中有关以下内容的页面:GeoJSON 对象。

以下样本文档中的 location.geo 字段来自 sample_mflix 数据库中的 theaters 集合,是一个描述影院坐标的 GeoJSON 点对象:

{ "_id" : ObjectId("59a47286cfa9a3a73e51e75c"), "theaterId" : 104, "location" : { "address" : { "street1" : "5000 W 147th St", "city" : "Hawthorne", "state" : "CA", "zipcode" : "90250" }, "geo" : { "type" : "Point", "coordinates" : [ -118.36559, 33.897167 ] } }}

以下示例使用 createIndexes() 方法对 sample_mflix 数据库的 theaters 集合的 location.geo 字段创建 2dsphere 索引,以启用地理空间搜索。

const database = client.db("sample_mflix");const movies = database.collection("movies"); /* Create a 2dsphere index on the "location.geo" field in the"movies" collection */const result = await movies.createIndex({ "location.geo": "2dsphere" }); // Print the result of the index creationconsole.log(`Index created: ${result}`);

MongoDB 还支持 2d 索引,用于计算欧几里德平面上的距离,以及处理 MongoDB 2.2 及更早版本中使用的“旧版坐标对”语法。要了解更多信息,请参阅地理空间查询。

唯一索引可确保索引字段不存储重复值。默认情况下,MongoDB 在创建集合期间会在 _id 字段上创建唯一索引。要创建唯一索引,请指定要防止重复的字段或字段组合,并将 unique 选项设置为 true。

以下示例使用 createIndex() 方法在 sample_mflix 数据库 theaters 集合中的 theaterId 字段上创建唯一索引。

const database = client.db("sample_mflix");const movies = database.collection("movies"); // Create a unique index on the "theaterId" field in the "theaters" collection.const result = await movies.createIndex({ theaterId: 1 }, { unique: true });console.log(`Index created: ${result}`);

如果您的写操作违反了唯一索引的规则,即尝试对索引字段存储重复的值, MongoDB 将抛出类似以下的报错:

E11000 duplicate key error index

要了解详情,请参阅唯一索引。

您可以使用 Node.js驱动程序以编程方式管理MongoDB Search 和MongoDB Vector Search 索引。

MongoDB搜索功能使您能够对MongoDB Atlas上托管的集合执行全文搜索。要了解有关 MongoDB 搜索 的更多信息,请参阅 MongoDB 搜索 文档。

MongoDB 向量搜索使您能够对存储在 Atlas 中的向量嵌入执行语义搜索。要学习;了解有关MongoDB Vector Search 的更多信息,请参阅MongoDB Vector Search 文档。

要学习;了解有关如何运行MongoDB Search 或MongoDB Vector Search查询的更多信息,请参阅运行MongoDB Search 查询或运行MongoDB Vector Search 查询指南。

以下部分包含演示如何管理MongoDB Search 和MongoDB Vector Search 搜索索引的代码示例。

您可以使用 createSearchIndex() 和 createSearchIndexes() 方法创建新的 MongoDB 搜索搜索索引 和 MongoDB Vector 搜索搜索索引。

以下代码演示如何使用 createSearchIndex() 方法创建名为 search1 的 MongoDB 搜索索引:

// Creates a MongoDB Search indexconst index1 = { name: "search1", definition: { "mappings": { "dynamic": true } }}await collection.createSearchIndex(index1);

连接到MongoDB Server v6.0.11 及更高版本时,您可以通过在索引定义的 type字段中指定 vectorSearch,使用驱动程序创建MongoDB 向量搜索索引。

以下代码演示如何使用 createSearchIndex() 方法创建MongoDB Vector Search索引:

// Creates a MongoDB Vector Search indexconst vectorSearchIdx = { name: "vsidx1", type: "vectorSearch", definition: { fields: [{ type: "vector", numDimensions: 384, path: "summary", similarity: "dotProduct" }] }} await collection.createSearchIndex(vectorSearchIdx);

您可以使用 listSearchIndexes() 方法来返回一个包含给定集合的MongoDB Search 和MongoDB Vector Search 搜索索引的游标。listSearchIndexes() 方法接受一个可选的 string 参数 name,以便仅返回名称匹配的索引。它还带有一个可选的 aggregateOptions 参数。

以下代码使用 listSearchIndexes() 方法列出集合中的 MongoDB 搜索 和 MongoDB 向量搜索 搜索索引:

// Lists search indexesconst result = await collection.listSearchIndexes().toArray();console.log("Existing search indexes:\n");for (const doc in result) { console.log(doc);}

您可以使用 updateSearchIndex() 方法通过提供新的索引定义来更新 MongoDB 搜索 或 MongoDB 向量搜索 索引。

以下代码演示如何使用 updateSearchIndex() 方法更新名为 search1 的MongoDB 搜索索引,将 description字段的类型更改为string:

// Updates a search indexconst index2 = { "mappings": { "dynamic": true, "fields": { "description": { "type": "string" } } }}await collection.updateSearchIndex("search1", index2);

您可以使用 dropSearchIndex() 方法删除 MongoDB 搜索 或 MongoDB 向量搜索 索引。

以下代码展示如何使用 dropSearchIndex() 方法删除名为 search1 的索引:

// Drops (deletes) a search indexawait collection.dropSearchIndex("search1");