Docs 菜单
Docs 主页
/ /

Multikey Indexes

键索引可提高对数组值字段的查询性能。 您可以使用 MongoDB\Collection::createIndex()方法和与创建单字段或复合索引相同的语法在集合上创建多键索引。

The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB deployment and load the sample datasets, see the MongoDB Get Started guide.

使用MongoDB\Collection::createIndex()方法创建多键索引。 以下示例在数组值cast字段上按升序创建索引:

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

以下是前面代码示例中创建的索引涵盖的查询示例:

$document = $collection->findOne(
['cast' => ['$in' => ['Aamir Khan', 'Kajol']]],
);
echo json_encode($document), PHP_EOL;
{"_id":...,"title":"Holi",...,"cast":["Ashutosh Gowariker",
"Aamir Khan","Rahul Ranade","Sanjeev Gandhi"],...}

多键索引在查询覆盖、索引绑定计算和排序行为方面的行为与其他索引不同。 要学习;了解有关多键索引的行为和限制的更多信息,请参阅MongoDB MongoDB Server手册中的多键索引。

要查看演示如何管理索引的可运行示例,请参阅《查询优化索引》

要进一步了解本指南所讨论的任何方法,请参阅以下 API 文档:

后退

复合索引

在此页面上