Case Insensitive Indexes不区分大小写的索引
On this page本页内容
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:要为区分大小写的索引指定排序规则,请包括:
locale
: specifies language rules.:指定语言规则。See Collation Locales for a list of available locales.有关可用区域设置的列表,请参阅排序规则区域设置。strength
: determines comparison rules.:确定比较规则。A value of值为1
or2
indicates a case insensitive collation.1
或2
表示不区分大小写的排序规则。
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
参数设置为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. strength
值。It does not use the collection's default collation or the index.它不使用集合的默认排序规则或索引。