Definition定义
cursor.collation(<collation document>)-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Specifies the collation for the cursor returned by the指定db.collection.find().db.collection.find()返回的游标的排序规则。To use, append to the要使用,请附加到db.collection.find().db.collection.find()。Thecursor.collation()accepts the following collation document:cursor.collation()接受以下排序规则文档:{
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}When specifying collation, the指定排序规则时,localefield is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.locale字段是必填的;所有其他排序字段都是可选的。有关字段的描述,请参阅排序规则文档。Field字段Type类型Description描述localestring字符串The ICU locale. See Supported Languages and Locales for a list of supported locales.ICU所在地。有关支持的区域设置列表,请参阅支持的语言和区域设置。To specify simple binary comparison, specify要指定简单的二进制比较,请指定localevalue of"simple"."simple"的locale值。strengthinteger整数Optional.可选。The level of comparison to perform. Corresponds to ICU Comparison Levels. Possible values are:要执行的比较级别。对应于ICU比较级别。可能的值有:Value值Description描述1 Primary level of comparison. Collation performs comparisons of the base characters only, ignoring other differences such as diacritics and case.初级比较。排序规则仅对基本字符进行比较,忽略其他差异,如变音符号和大小写。2 Secondary level of comparison. Collation performs comparisons up to secondary differences, such as diacritics. That is, collation performs comparisons of base characters (primary differences) and diacritics (secondary differences). Differences between base characters takes precedence over secondary differences.二级比较。排序执行比较,直到次要差异,如变音符号。也就是说,排序规则执行基本字符(主要差异)和变音符号(次要差异)的比较。基本字符之间的差异优先于次要差异。3 Tertiary level of comparison. Collation performs comparisons up to tertiary differences, such as case and letter variants. That is, collation performs comparisons of base characters (primary differences), diacritics (secondary differences), and case and variants (tertiary differences). Differences between base characters takes precedence over secondary differences, which takes precedence over tertiary differences.第三级比较。排序可以进行高达三级差异的比较,如大小写和字母变体。也就是说,排序规则执行基本字符(主要差异)、变音符号(次要差异)以及大小写和变体(第三差异)的比较。基本字符之间的差异优先于次要差异,次要差异优先于第三级差异。This is the default level.这是默认级别。4 Quaternary Level. Limited for specific use case to consider punctuation when levels 1-3 ignore punctuation or for processing Japanese text.第四纪水平。仅限于特定用例,当级别1-3忽略标点符号或处理日语文本时,考虑标点符号。5 Identical Level. Limited for specific use case of tie breaker.相同级别。仅限于断路器的特定使用情况。See ICU Collation: Comparison Levels for details.详情请参阅ICU排序:比较级别。caseLevelboolean布尔值Optional.可选。Flag that determines whether to include case comparison at用于确定是在strengthlevel1or2.strength级别1还是2包括大小写比较的标志。If如果为true, include case comparison; i.e.true,则包括大小写比较;即。When used with当使用strength:1, collation compares base characters and case.strength:1时,排序规则会比较基本字符和大小写。When used with当使用strength:2, collation compares base characters, diacritics (and possible other secondary differences) and case.strength:2时,排序规则会比较基本字符、变音符号(以及可能的其他次要差异)和大小写。
If如果为false, do not include case comparison at level1or2. The default isfalse.false,则不包括级别1或2的大小写比较。默认值为false。For more information, see ICU Collation: Case Level.请参阅ICU排序规则:大小写级别以了解详细信息。caseFirststring字符串Optional.可选。A field that determines sort order of case differences during tertiary level comparisons.在三级比较期间确定大小写差异排序顺序的字段。Possible values are:可能的值有:Value值Description描述"upper" Uppercase sorts before lowercase.大写排序在小写排序之前。"lower" Lowercase sorts before uppercase.小写排序在大写排序之前。"off" Default value. Similar to默认值。与"lower"with slight differences."lower"相似,略有差异。See https://unicode-org.github.io/icu/userguide/strings/properties.html#customization for details of differences.请参阅https://unicode-org.github.io/icu/userguide/strings/properties.html#customization以了解差异的详细信息。numericOrderingboolean布尔值Optional.可选。Flag that determines whether to compare numeric strings as numbers or as strings.用于确定是将数字字符串比较为数字还是字符串的标志。If如果为true, compare as numbers; i.e."10"is greater than"2".true,则作为数字进行比较;即"10"大于"2"。If如果为false, compare as strings; i.e."10"is less than"2".false,则作为字符串进行比较;即"10"小于"2"。Default is默认值为false.false。alternatestring字符串Optional.可选。Field that determines whether collation should consider whitespace and punctuation as base characters for purposes of comparison.用于确定排序规则是否应将空格和标点符号视为比较的基本字符的字段。Possible values are:可能的值有:Value值Description描述"non-ignorable"Whitespace and punctuation are considered base characters.空格和标点符号被视为基本字符。"shifted"Whitespace and punctuation are not considered base characters and are only distinguished at strength levels greater than 3.空格和标点符号不被视为基本字符,只有在强度级别大于3时才能区分。See ICU Collation: Comparison Levels for more information.请参阅ICU排序规则:比较级别以了解详细信息。Default is
"non-ignorable".maxVariablestring字符串Optional.可选。Field that determines up to which characters are considered ignorable when用于确定在alternate: "shifted". Has no effect ifalternate: "non-ignorable"alternate: "shifted"时最多哪些字符被视为可忽略的字段。如果alternate: "non-ignorable"则无效Possible values are:可能的值有:Value值Description描述"punct"Both whitespaces and punctuation are "ignorable", i.e. not considered base characters.空格和标点符号都是“可忽略的”,即不被视为基本字符。"space"Whitespace are "ignorable", i.e. not considered base characters.空格是“可忽略的”,即不被视为基本字符。backwardsboolean布尔值Optional.可选。Flag that determines whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.用于确定带有变音符号的字符串是否从字符串后面排序的标志,例如使用某些法语词典排序。If如果为true, compare from back to front.true,从后到前进行比较。If如果为false, compare from front to back.false,请从前到后进行比较。The default value is默认值为false.false。normalizationboolean布尔值Optional.可选。Flag that determines whether to check if text require normalization and to perform normalization. Generally, majority of text does not require this normalization processing.确定是否检查文本是否需要规范化并执行规范化的标志。一般来说,大多数文本不需要这种规范化处理。If如果为true, check if fully normalized and perform normalization to compare text.true,请检查是否完全规范化,并执行规范化以比较文本。If如果为false, does not check.false,则不进行检查。The default value is默认值为false.false。See https://unicode-org.github.io/icu/userguide/collation/concepts.html#normalization for details.请参阅https://unicode-org.github.io/icu/userguide/collation/concepts.html#normalization以了解详细信息。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Examples示例
Consider a collection 考虑一个包含以下文档的集合foo with the following documents:foo:
{ "_id" : 1, "x" : "a" }
{ "_id" : 2, "x" : "A" }
{ "_id" : 3, "x" : "á" }
The following operation specifies a query filter of 以下操作指定了x: "a". The operation also includes a collation option with locale: "en_US" (US English locale) and strength: 1 (compare base characters only; i.e. ignore case and diacritics):x: "a"的查询筛选器。该操作还包括一个排序规则选项,其locale: "en_US"(美国英语语言环境),strength: 1(仅比较基本字符;即忽略大小写和变音符号):
db.foo.find( { x: "a" } ).collation( { locale: "en_US", strength: 1 } )
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 1, "x" : "a" }
{ "_id" : 2, "x" : "A" }
{ "_id" : 3, "x" : "á" }
If you do not specify the collation, i.e. 如果不指定排序规则,即db.collection.find( { x: "a" } ), the query would only match the following document:db.collection.find( { x: "a" } ),查询将只匹配以下文档:
db.foo.find( { x: "a" } )
You can chain other cursor methods, such as 您可以将其他游标方法(如cursor.sort() and cursor.count(), to cursor.collation():cursor.sort()和cursor.count())链接到cursor.collation():
db.collection.find({...}).collation({...}).sort({...});
db.collection.find({...}).collation({...}).count();
Note
You cannot specify multiple collations for an operation. For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort.不能为操作指定多个排序规则。例如,您不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能对查找使用一个排序规则,对排序使用另一个。