On this page本页内容
cursor.collation(<collation document>)
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
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()
。
The cursor.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 指定排序规则时,locale
field is mandatory; all other collation fields are optional. locale
字段是必需的;所有其他排序字段都是可选的。For descriptions of the fields, see Collation Document.有关字段的描述,请参阅排序规则文档。
locale | string |
| ||||||||||||
strength | integer |
| ||||||||||||
caseLevel | boolean |
| ||||||||||||
caseFirst | string |
| ||||||||||||
numericOrdering | boolean |
| ||||||||||||
alternate | string |
| ||||||||||||
maxVariable | string |
| ||||||||||||
backwards | boolean |
| ||||||||||||
normalization | boolean |
|
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"
. 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):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();
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.例如,不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能将一个排序规则用于查找,而将另一个用于排序。