Case Insensitive Indexes不区分大小写的索引

On this page本页内容

New in version 3.4.版本3.4中的新功能。

Case insensitive indexes support queries that perform string comparisons without regard for case.不区分大小写的索引支持在不考虑大小写的情况下执行字符串比较的查询。

You can create a case insensitive index with db.collection.createIndex() by specifying the collation parameter as an option. 通过将collation参数指定为选项,可以使用db.collection.createIndex()创建不区分大小写的索引。For example:例如:

db.collection.createIndex( { "key" : 1 },
                           { collation: {
                               locale : <locale>,
                               strength : <strength>
                             }
                           } )

To specify a collation for a case sensitive index, include:要为区分大小写的索引指定排序规则,请包括:

For additional collation fields, see Collation.有关其他排序规则字段,请参阅排序规则

Behavior行为

Using a case insensitive index does not affect the results of a query, but it can increase performance; see Indexes for a detailed discussion of the costs and benefits of indexes.使用不区分大小写的索引不会影响查询结果,但可以提高性能;有关索引的成本和收益的详细讨论,请参阅索引

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参数设置为12(有关strength参数的详细说明,请参阅排序规则)。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.insert( [ { 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.insert( [ { 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. 上面的操作只找到一个文档,因为它使用的排序规则没有指定strength值。It does not use the collection’s default collation or the index.它不使用集合的默认排序规则或索引。