Case-insensitive indexes support queries that perform string comparisons without regard for case. Case insensitivity is derived from collation.不区分大小写的索引支持执行字符串比较而不考虑大小写的查询。不区分大小写源于排序规则。
Important
Case-insensitive indexes do not improve performance for 不区分大小写的索引并不能提高$regex queries, as the $regex operator is not collation-aware and therefore cannot take advantage of such indexes.$regex查询的性能,因为$regex运算符不支持排序规则,因此无法利用此类索引。
Command Syntax命令语法
You can create a case-insensitive index with 通过指定db.collection.createIndex() by specifying the collation option:collation选项,您可以使用db.collection.createIndex()创建不区分大小写的索引:
db.collection.createIndex(
{
<field>: <sortOrder>
},
{
collation:
{
locale : <locale>,
strength : < 1 | 2 >
}
}
)
To specify a collation for a case-insensitive index, include the following fields in the 要为不区分大小写的索引指定排序规则,请在collation object:collation对象中包含以下字段:
locale | |
strength | strength value of 1 or 2 indicates case-insensitive collation.strength值为1或2表示不区分大小写的排序规则。 |
For additional collation fields, see Collation.有关其他排序规则字段,请参阅排序规则。
Behavior行为
To use an index that specifies a collation, query and sort operations must specify the same collation as the index. If a collection has defined a collation, all queries and indexes inherit that collation unless they explicitly specify a different collation.若要使用指定排序规则的索引,查询和排序操作必须指定与索引相同的排序规则。如果集合定义了排序规则,则所有查询和索引都将继承该排序规则,除非它们明确指定了不同的排序规则。
Examples示例
Create a Case-Insensitive Index创建大小写不敏感索引
To use a case-insensitive index on a collection with no default collation, create an index with a collation and set the 要在没有默认排序规则的集合上使用不区分大小写的索引,请创建一个具有排序规则的索引,并将strength parameter to 1 or 2 (see Collation for a detailed description of the strength parameter). strength(强度)参数设置为1或2(有关强度参数的详细说明,请参阅排序规则)。You must specify the same collation at the query level in order to use the index-level collation.您必须在查询级别指定相同的排序规则,才能使用索引级别的排序规则。
The following example creates a collection with no default collation, then adds an index on the 以下示例创建了一个没有默认排序规则的集合,然后在type field with a case-insensitive collation.type字段上添加了一个不区分大小写排序规则的索引。
db.createCollection("fruit")
db.fruit.createIndex(
{ type: 1 },
{ collation: { locale: 'en', strength: 2 } }
)
To use the index, queries must specify the same collation.要使用索引,查询必须指定相同的排序规则。
db.fruit.insertMany( [
{ type: "apple" },
{ type: "Apple" },
{ type: "APPLE" }
] )
db.fruit.find( { type: "apple" } ) // does not use index, finds one result不使用索引,找到一个结果
db.fruit.find( { type: "apple" } ).collation( { locale: 'en', strength: 2 } )
// uses the index, finds three results使用索引,找到三个结果
db.fruit.find( { type: "apple" } ).collation( { locale: 'en', strength: 1 } )
// does not use the index, finds three results不使用索引,找到三个结果Case-Insensitive Indexes on Collections with a Default Collation对具有默认排序的集合不敏感的索引
When you create a collection with a default collation, all the indexes you create subsequently inherit that collation unless you specify a different collation. All queries which do not specify a different collation also inherit the default collation.使用默认排序规则创建集合时,除非指定其他排序规则,否则随后创建的所有索引都将继承该排序规则。所有未指定其他排序规则的查询也将继承默认排序规则。
The following example creates a collection called 以下示例使用默认排序规则创建一个名为names with a default collation, then creates an index on the first_name field.names的集合,然后在first_name字段上创建索引。
db.createCollection("names", { collation: { locale: 'en_US', strength: 2 } } )
db.names.createIndex( { first_name: 1 } ) // inherits the default collation继承默认排序规则
Insert a small collection of names:插入一小部分姓名:
db.names.insertMany( [
{ first_name: "Betsy" },
{ first_name: "BETSY"},
{ first_name: "betsy"}
] )
Queries on this collection use the specified collation by default, and if possible use the index as well.默认情况下,对此集合的查询使用指定的排序规则,如果可能的话,也使用索引。
db.names.find( { first_name: "betsy" } )
// inherits the default collation:继承默认排序规则: { collation: { locale: 'en_US', strength: 2 } }
// finds three results找到三个结果
The above operation uses the collection's default collation and finds all three documents. It uses the index on the 上述操作使用集合的默认排序规则并查找所有三个文档。它使用first_name field for better performance.first_name字段上的索引以获得更好的性能。
It is still possible to perform case sensitive searches on this collection by specifying a different collation in the query:通过在查询中指定不同的排序规则,仍然可以对此集合执行区分大小写的搜索:
db.names.find( { first_name: "betsy" } ).collation( { locale: 'en_US' } )
// does not use the collection's default collation, finds one result不使用集合的默认排序规则,只查找一个结果
The above operation finds only one document, because it uses a collation with no 上述操作只找到一个文档,因为它使用的排序规则没有指定strength value specified. It does not use the collection's default collation or the index.strength(强度)值。它不使用集合的默认排序规则或索引。