CosmsDB 查询日期字段

CosmsDB Query on Date Field

当我 运行 在 Azure 门户上执行以下查询时,它 运行 没问题,但是当 运行 从 .NET 应用程序以编程方式进行时,它给出了一个错误:

Azure CosmosDB 门户上的原始查询:SELECT * FROM c where c.OrderDateTime > '2018-11-29T18:33:17.5957307Z'工作正常

string queryString = $"SELECT * FROM c  Where c.OrderDateTime >= '{DateTime.UtcNow.AddDays(-1).ToString("s")}'"; Gives the following error:

消息:

{\"Errors\":[\"An invalid query has been specified with filters against path(s) that are not range-indexed. Consider adding allow scan header in the request.\"]}

我了解 运行ge 查询中的日期需要与字符串不同的处理方式,但想知道它如何在门户上工作?我的代码中是否遗漏了其他内容?

{\"Errors\":[\"An invalid query has been specified with filters against path(s) that are not range-indexed. Consider adding allow scan header in the request.\"]}

您在此处收到的错误表明查询引擎找不到路径 C.OrderDateTime 的范围索引。请检查您 collection 的 Indexing Policy 并确保数字和字符串都被索引为范围而不是散列。有关索引策略的更多信息,请参阅 official document

或者您可以在 FeedOptions 中将 EnableScanInQuery 设置为 true 并遵循错误 details:Consider adding allow scan header in the request。请参考here.

var feedOptions = new FeedOptions
{
   EnableScanInQuery = true,
};

据我所知,SQL 门户上的查询不同于 sdk 查询或 rest api 查询。它将有一些隐式优化,例如不必提供分区键来对分区键执行查询 column.That 并不意味着不需要在客户端查询操作中提供分区键。