Create a Collection with Collation使用排序规则创建集合
On this page本页内容
Collation allows you to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.排序规则允许您为字符串比较指定特定于语言的规则,例如大小写和重音标记的规则。
Procedure过程
Select a value for locale.选择“区域设置”的值。
You are required to select a locale from the MongoDB supported languages.您需要从MongoDB支持的语言中选择一个“区域设置”。
All other collation options parameters are optional. 所有其他排序规则选项参数都是可选的。For descriptions of the fields, see Collation.有关字段的描述,请参阅排序规则。
Restrictions and Limitations限制和局限性
The following restrictions apply when the parameter 当参数numericOrdering
is set to true
:numericOrdering
设置为true
时,以下限制适用:
Only contiguous non-negative integer substrings of digits are considered in the comparisons.在比较中只考虑数字的连续非负整数子串。numericOrdering
does not support:numericOrdering
不支持:+
-
- exponents
Only Unicode code points in the Number or Decimal Digit (Nd) category are treated as digits.只有数字或十进制数字(Nd)类别中的Unicode代码点被视为数字。If the number length exceeds 254 characters, the excess characters are treated as a separate number.如果数字长度超过254个字符,则多余的字符将被视为单独的数字。
Example实例
Consider a collection with the following string number and decimal values:考虑具有以下字符串编号和十进制值的集合:
[
{ "n": "1" },
{ "n": "2" },
{ "n": "-2.1" },
{ "n": "2.0" },
{ "n": "2.20" },
{ "n": "10"},
{ "n": "20" },
{ "n": "20.1" },
{ "n": "-10" },
{ "n": "3" }
]
The following find query uses a collation document containing the 以下查找查询使用包含numericOrdering
parameter:numericOrdering
参数的排序规则文档:
db.c.find(
{ }, { _id: 0 }
).sort(
{ n: 1 }
).collation( {
locale: 'en_US',
numericOrdering: true
} )
For more information on querying documents in Compass, see Query Your Data.有关在Compass中查询文档的详细信息,请参阅查询您的数据。
The operations returns the following results:操作返回以下结果:
[
{ "n": "-2.1" },
{ "n": "-10" },
{ "n": "1" },
{ "n": "2" },
{ "n": "2.0" }
{ "n": "2.20" },
{ "n": "3" },
{ "n": "10" },
{ "n": "20" },
{"n": "20.1" }
]
numericOrdering: true
sorts the string values in ascending order as if they were numeric values.numericOrdering: true
按升序对字符串值进行排序,就好像它们是数值一样。The two negative values两个负值-2.1
and-10
are not sorted in the expected sort order because they have unsupported-
characters.-2.1
和-10
没有按预期的排序顺序排序,因为它们包含不支持的-
字符。