不分大小写索引

    在本页面

    3.4版本新功能

    不区分大小写索引支持执行不考虑大小写的字符串比较的查询。

    通过指定“collation”参数作为选项,可以使用‘ db.collection.createIndex() ‘创建大小写不敏感索引。例如:

    1. db.collection.createIndex( { "key" : 1 },
    2. { collation: {
    3. locale : <locale>,
    4. strength : <strength>
    5. }
    6. } )

    要为区分大小写的索引指定排序规则,请包括:

    • locale:指定语言规则。参见Collation locale获取可用locale列表。
    • strength:确定比较规则。值“1”或“2”表示排序规则不区分大小写。

    有关其他排序字段,请参见collation

    行为

    使用不区分大小写的索引不会影响查询的结果,但可以提高性能;请参阅Indexes以获得关于索引成本和收益的详细讨论。

    若要使用指定排序规则的索引,查询和排序操作必须指定与索引相同的排序规则。如果集合定义了排序规则,所有查询和索引都会继承该排序规则,除非它们显式指定不同的排序规则。

    例子

    创建不区分大小写的索引

    使用一个不分大小写指数在一组没有默认排序,创建一个索引排序和“strength”参数设置为“1”或“2”(见排序的strength参数的详细描述)。必须在查询级别指定相同的排序规则,才能使用索引级别的排序规则。

    下面的示例创建一个没有默认排序规则的集合,然后在“type”字段上使用大小写不敏感排序规则添加索引。

    1. db.createCollection("fruit")
    2. db.fruit.createIndex( { type: 1},
    3. { collation: { locale: 'en', strength: 2 } } )

    要使用索引,查询必须指定相同的排序规则。

    1. db.fruit.insert( [ { type: "apple" },
    2. { type: "Apple" },
    3. { type: "APPLE" } ] )
    4. db.fruit.find( { type: "apple" } ) // does not use index, finds one result
    5. db.fruit.find( { type: "apple" } ).collation( { locale: 'en', strength: 2 } )
    6. // uses the index, finds three results
    7. db.fruit.find( { type: "apple" } ).collation( { locale: 'en', strength: 1 } )
    8. // does not use the index, finds three results

    具有默认排序规则的集合中的区分大小写的索引

    使用默认排序规则创建集合时,除非指定不同的排序规则,否则随后创建的所有索引都会继承该排序规则。所有没有指定不同排序规则的查询也继承默认排序规则。

    下面的示例使用默认排序规则创建名为“names”的集合,然后在“first_name”字段上创建索引。

    1. db.createCollection("names", { collation: { locale: 'en_US', strength: 2 } } )
    2. db.names.createIndex( { first_name: 1 } ) // inherits the default collation

    插入少量名称:

    1. db.names.insert( [ { first_name: "Betsy" },
    2. { first_name: "BETSY"},
    3. { first_name: "betsy"} ] )

    对该集合的查询默认情况下使用指定的排序规则,如果可能还使用索引。

    1. db.names.find( { first_name: "betsy" } )
    2. // inherits the default collation: { collation: { locale: 'en_US', strength: 2 } }
    3. // finds three results

    上述操作使用集合的默认排序规则并查找所有三个文档。它使用’ first_name ‘字段上的索引以获得更好的性能。

    通过在查询中指定不同的排序规则,仍然可以对这个集合执行区分大小写的搜索:

    1. db.names.find( { first_name: "betsy" } ).collation( { locale: 'en_US' } )
    2. // does not use the collection's default collation, finds one result

    上面的操作只找到一个文档,因为它使用的排序规则没有指定strength值。它不使用集合的默认排序规则或索引。