【问题标题】:How can I order by multiple columns using the IQueryable in Cosmos DB?如何使用 Cosmos DB 中的 IQueryable 按多列排序?
【发布时间】:2021-12-30 01:42:01
【问题描述】:

如何使用 Cosmos DB 中的 IQueryable 按多列排序?

我尝试执行以下操作:

books.OrderBy(book => book.Author).ThenBy(book => book.Name);

它适用于 Cosmos DB 模拟器,但不适用于真实的 DB。为什么?

这是我得到的错误:

The order by query does not have a corresponding composite index that it can be served from.

【问题讨论】:

  • 你想达到什么目的?查询看起来不错?来源是什么,预期的结果是什么?
  • @SebastianSiemens,我正在尝试按一列排序,然后如果第一列的值与第二列相同。在哪里可以找到 Cosmos DB 错误的日志?

标签: c# linq azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

这不是您的 C# 代码的问题。如果您想在需要索引的属性上使用ORDER BY。对于多个列,它是相同的,尽管您需要以完全相同的顺序出现在 ORDER BY 子句中的所有属性的复合索引。

对于您的用例,您需要将以下索引添加到您的 Cosmos DB 容器中:

"compositeIndexes": [
    [
        {
            "path": "/Author",
            "order": "ascending"
        },
        {
            "path": "/Name",
            "order": "ascending"
        }
    ]
]

【讨论】:

  • 另外,我发现this 文档很有用。因为,在我的情况下,我不允许直接编辑生产数据库,而必须使用 SDK 编写迁移。
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 2022-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 2021-12-16
相关资源
最近更新 更多