文档菜单
文档首页
/ / /
PHP 库手册

使用索引优化查询

本页内容

  • 概述
  • 示例应用程序
  • 单字段索引
  • 复合索引
  • 多键索引
  • 地理空间索引
  • 唯一索引
  • 通配符索引
  • 聚集索引
  • 文本索引
  • 列出索引
  • 删除索引
  • Atlas 搜索索引管理
  • 创建搜索索引
  • 列出搜索索引
  • 更新搜索索引
  • 删除搜索索引

在此页面上,您可以查看可复制的代码示例,展示如何使用MongoDB PHP库管理不同类型的索引。

提示

要了解有关使用索引的更多信息,请参阅索引考虑与管理指南。要了解此页面上显示的任何索引的更多信息,请参阅每个部分中提供的链接。

要从此页面使用示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请确保将环境变量MONGODB_URI设置为您的MongoDB部署的连接字符串,并用您的目标命名空间值替换<database><collection>占位符。

您可以使用以下示例应用程序来测试此页面上的代码示例。要使用示例应用程序,请执行以下步骤

  1. 确保您已在项目中安装MongoDB PHP库。要了解有关安装MongoDB PHP库的更多信息,请参阅下载和安装指南。

  2. 复制以下代码并将其粘贴到新的.php文件中。

  3. 将此页面的代码示例复制到文件中的指定行。

1<?php
2
3require __DIR__ . '/../vendor/autoload.php';
4
5$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6$client = new MongoDB\Client($uri);
7
8$collection = $client->selectCollection('<database>', '<collection>');
9
10// Start example code here
11
12// End example code here

一些示例使用toJSON()函数将更改事件(作为BSON文档)表示为扩展JSON。要使用此函数,请将以下代码粘贴到您的应用程序文件中

function toJSON(object $document): string
{
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
}

以下示例在指定的字段上创建一个升序索引

$indexName = $collection->createIndex(['<field name>' => 1]);

要了解更多关于单字段索引的信息,请参阅单字段索引指南。

以下示例在指定的字段上创建由两个升序索引组成的复合索引

$indexName = $collection->createIndex(
['<field name 1>' => 1, '<field name 2>' => 1]
);

要了解更多关于复合索引的信息,请参阅复合索引指南。

以下示例在指定的数组值字段上创建一个升序多键索引

$indexName = $collection->createIndex(['<array field name>' => 1]);

要了解更多关于多键索引的信息,请参阅多键索引指南。

以下示例在具有GeoJSON对象值的指定字段上创建一个2dsphere索引

$indexName = $collection->createIndex(
[ '<GeoJSON object field>' => '2dsphere']
);

要了解更多关于GeoJSON数据类型的信息,请参阅MongoDB服务器手册中的GeoJSON对象

以下示例在指定字段上创建一个升序唯一索引

$indexName = $collection->createIndex(['<field name>' => 1], ['unique' => true]);

以下示例在集合上创建一个升序通配符索引

$indexName = $collection->createIndex(['$**' => 1]);

在指定数据库中创建新集合时,您可以创建聚集索引。以下示例在 _id 字段上创建一个升序聚集索引的新集合

$options = [
'clusteredIndex' => [
'key' => ['_id' => 1],
'unique' => true
]
];
$database->createCollection('<collection name>', $options);

以下示例在指定的字符串字段上创建一个文本索引

$indexName = $collection->createIndex(['<field name>' => 'text']);

以下示例打印指定集合中索引的列表

foreach ($collection->listIndexes() as $indexInfo) {
echo $indexInfo;
}

以下示例删除具有指定名称的索引

$collection->dropIndex('<index name>');

要了解更多关于删除索引的信息,请参阅索引考虑和管理指南中的删除索引

以下部分包含代码示例,描述了如何管理 Atlas搜索索引。

注意

Atlas搜索索引管理是异步的

MongoDB PHP库异步管理Atlas搜索索引。以下各节中描述的库方法立即返回服务器响应,但您搜索索引的更改将在后台进行,可能需要一段时间才能完成。

要了解有关Atlas搜索索引的更多信息,请参阅Atlas搜索索引指南。

以下示例在指定的字段上创建Atlas搜索索引

$indexName = $collection->createSearchIndex(
['mappings' => ['dynamic' => true]],
['name' => '<Search index name>']
);

以下示例打印指定集合中Atlas搜索索引的列表

foreach ($collection->listSearchIndexes() as $indexInfo) {
echo toJSON($indexInfo), PHP_EOL;
}

以下示例使用指定的索引定义更新现有的Atlas Search索引

$collection->updateSearchIndex(
'<Search index name>',
['mappings' => [
'dynamic' => false,
'fields' => [
'<string field name>' => [
'type' => 'string',
'analyzer' => 'lucene.standard'
]
]
]]
);

以下示例删除指定名称的Atlas Search索引

$collection->dropSearchIndex('<Search index name>');

返回

数据聚合