Docs HomeMongoDB Manual

Create a View with Default Collation创建具有默认排序规则的视图

Collation排序规则 allows you to specify language-specific rules for string comparison, such as rules for letter-case and accent marks.允许您为字符串比较指定特定于语言的规则,例如字母大小写和重音标记的规则。

This page explains how to specify a default collation for a view.本页介绍如何为视图指定默认排序规则。

Example实例

Create a places collection with the following documents:使用以下文档创建places集合:

db.places.insertMany([
{ _id: 1, category: "café" },
{ _id: 2, category: "cafe" },
{ _id: 3, category: "cafE" }
])

The following operation creates a view, specifying collation at the view level:以下操作创建一个视图,在视图级别指定排序规则:

db.createView(
"placesView",
"places",
[ { $project: { category: 1 } } ],
{ collation: { locale: "fr", strength: 1 } }
)

The following operation uses the view's collation:以下操作使用视图的排序规则:

db.placesView.countDocuments( { category: "cafe" } )

The operation returns 3.操作返回3

Note

Collation Behavior排序规则行为

  • You can specify a default collation for a view at creation time. 可以在创建时为视图指定默认排序规则If no collation is specified, the view's default collation is the "simple" binary comparison collator. 如果未指定排序规则,则视图的默认排序规则是“简单”二进制比较排序规则。That is, the view does not inherit the collection's default collation.也就是说,视图不继承集合的默认排序规则。
  • String comparisons on the view use the view's default collation. 视图上的字符串比较使用视图的默认排序规则。An operation that attempts to change or override a view's default collation will fail with an error.尝试更改或覆盖视图的默认排序规则的操作将失败,并出现错误。
  • If creating a view from another view, you cannot specify a collation that differs from the source view's collation.如果从其他视图创建视图,则不能指定与源视图的排序规则不同的排序规则。
  • If performing an aggregation that involves multiple views, such as with $lookup or $graphLookup, the views must have the same collation.如果执行涉及多个视图的聚合,例如使用$lookup$graphLookup,则这些视图必须具有相同的排序规则