【问题标题】:in cosmosDB/MongoDB API sorting by string fields containing numbers with the collation option numericOrdering does not work在 cosmosDB/MongoDB API 中,使用排序选项 numericOrdering 按包含数字的字符串字段进行排序不起作用
【发布时间】:2021-12-01 09:13:53
【问题描述】:

在 cosmosDB MongoDB API 中,我需要运行一个查询来检索按字符串字段排序的项目,该字段还可以包含数字,例如 item-2、item-3、item-11。在 MongoDB 文档中,默认行为是对 [item-11, item-2, item-3] 之类的项目进行排序,将它们作为字符串进行比较,但是从 3.4 版开始,可以以这种方式使用排序规则:

db.collectionName.find().sort({title: 1}).collat​​ion({locale: "en_US", numericOrdering: true})

将子字符串中的数字排序为数字(在我的情况下为 [item-2, item-3, item-11]);

我的 CosmosDB 服务器版本是 3.6,因此它应该支持这一点,因为它是在 MongoDB 3.4 中添加的,但排序规则似乎被忽略了(使用 Compass 测试)。

有没有办法使这项工作或有关此问题的一些文档参考?

【问题讨论】:

    标签: azure-cosmosdb azure-cosmosdb-mongoapi


    【解决方案1】:

    事实证明,我们在我试图排序的字段上设置了索引;这些索引是在没有排序规则的情况下创建的,如 [MongoDB 官方文档][1] 中所述

    [1]:https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/#index-use-and-collation,为了在索引上使用排序规则进行排序,必须使用相同的排序规则创建索引。

    更新:

    即使这似乎是一个错误的线索,我尝试使用排序规则创建索引:

       db.test_documents.createIndex( { title: 1 }, { collation: { locale: "en", numericOrdering: true } } )
    

    我收到了错误:

    {
        "message" : "collation",
        "ok" : 0,
        "code" : 115,
        "codeName" : "CommandNotSupported",
        "name" : "MongoError"
    }
    

    在 Azure CosmosDB 文档中也找不到任何对排序规则的引用,目前我认为它不受支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2015-12-13
      • 2021-03-17
      • 2017-08-11
      • 1970-01-01
      • 2019-05-11
      相关资源
      最近更新 更多