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
strength
1
or 2
indicates a case insensitive collation.1
或2
表示不区分大小写的排序规则。For additional collation fields, see Collation.有关其他排序规则字段,请参阅排序规则。
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.如果集合定义了排序规则,则所有查询和索引将继承该排序规则,除非它们显式指定不同的排序规则。
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
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.它不使用集合的默认排序规则或索引。