对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

创建带排序规则的集合

Collation allows you to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.

当参数 numericOrdering 设置为 true 时,以下限制适用:

  • 在比较中,仅考虑数字的连续非负整数子字符串。numericOrdering 不支持:

    • +

    • -

    • 指数

  • 只有数字或十进制数字 (Nd) 类别中的 Unicode 代码点才被视为数字。

  • 如果数字长度超过 254 个字符,则超出的字符将被视为单独的数字。

1

在 Collections 屏幕中点击 Create Collection 按钮。

2
3

选择 Use Custom Collaton(使用自定义排序规则)选项。

4

You are required to select a locale from the MongoDB supported languages.

All other collation options parameters are optional. For descriptions of the fields, see Collation.

5

请考虑一个具有以下字符串数字和十进制值的集合:

[
{ "n": "1" },
{ "n": "2" },
{ "n": "-2.1" },
{ "n": "2.0" },
{ "n": "2.20" },
{ "n": "10"},
{ "n": "20" },
{ "n": "20.1" },
{ "n": "-10" },
{ "n": "3" }
]

以下 find 查询使用包含 numericOrdering 参数的排序规则文档:

db.c.find(
{ }, { _id: 0 }
).sort(
{ n: 1 }
).collation( {
locale: 'en_US',
numericOrdering: true
} )

有关在 Compass 中查询文档的更多信息,请参阅查询数据。

操作返回以下结果:

[
{ "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 按升序对字符串值进行排序(类似于对数字值的排序)。

  • 两个负值 -2.1 和 -10 没有按照预期的排序顺序进行排序,因为它们包含不支持的 - 字符。

给本页内容打分

在此页面上