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:例如:
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
(有关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
字段上添加一个不区分大小写排序规则的索引。
To use the index, queries must specify the same 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
字段上创建索引。
Insert a small collection of names:插入一小部分名称:
Queries on this collection use the specified collation by default, and if possible use the index as well.默认情况下,此集合上的查询使用指定的排序规则,如果可能,还可以使用索引。
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:通过在查询中指定不同的排序规则,仍然可以对此集合执行区分大小写的搜索:
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.它不使用集合的默认排序规则或索引。