azure-cosmosdb - Cosmos DB 数组查询在 .NET SDK 中不起作用

标签 azure-cosmosdb azure-cosmosdb-sqlapi

我正在尝试获取容器中使用的所有不同类别值,其中类别是字符串数组。

查看 Cosmos DB SQL 文档,我构建了以下查询,当我执行 SQL 时,该查询在模拟器中运行,并按预期返回不同类别的数组。

SELECT DISTINCT * FROM c IN s.categories

结果

[
    "Running",
    "Rugby",
    "Icon"
]

但是,如果我通过 Cosmos .NET SDK 使用相同的查询,则会收到错误

"Identifier 'session' could not be resolved."

为什么此查询可以在模拟器中运行,但不能通过 SDK 运行?是我做错了什么还是 SDK 的问题?

示例数据:

{
"name": "Sample Entity",
"description": "<p></p>",
"startDateUtc": "2020-07-13T19:10:00Z",
"endDateUtc": "2020-07-13T20:00:00Z",
"categories": [
    "Running",
    "Icon"
],
"id": "h3foyhfk0a3betj1",
"dateCreated": "2020-07-15T06:33:57.9406583Z",
"lastUpdated": "2020-08-13T17:04:18.6182079Z" }

SDK代码:

var query = new QueryDefinition("SELECT DISTINCT * FROM c IN s.categories");
var results = new List<TEntity>();
            
var resultSetIterator = _container.GetItemQueryIterator<TEntity>(query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(partitionKey) });
while (resultSetIterator.HasMoreResults)
{
      var response = await resultSetIterator.ReadNextAsync();
      results.AddRange(response);
      if (response.Diagnostics != null)
      {
           _logger.LogTrace($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics}");
      }
}

return results;

最佳答案

删除 , requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(partitionKey)

enter image description here

然后你就会得到结果。使用时requestOptions可能有错误。

var query = new QueryDefinition("SELECT DISTINCT * FROM c IN s.categories");
var results = new List<dynamic>();

var resultSetIterator = container.GetItemQueryIterator<dynamic>(query);
while (resultSetIterator.HasMoreResults)
{
    var response = await resultSetIterator.ReadNextAsync();
    results.AddRange(response);
    if (response.Diagnostics != null)
    {
        Console.WriteLine($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics}");
    }
}

关于azure-cosmosdb - Cosmos DB 数组查询在 .NET SDK 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63402515/

相关文章:

spring-boot - Azure cosmos 依赖项问题和 AzureServiceBusMessagingAutoConfiguration$ServiceBusTemplateConfiguration NamespaceProperties 未找到

azure - 添加新集合时对 Azure Cosmos DB 模拟器的高要求

azure-cosmosdb - 了解 fold() 及其对 Azure Cosmos DB 中 gremlin 查询成本的影响

azure - 关于 Cosmos DB 物理分区和逻辑分区的一些问题

azure-cosmosdb - 如何使用 PHP 使用 Azure documentdb

azure - 无法连接 CosmosDB 作为流分析输出

c# - CosmosDb 按 slug 获取项目

azure - 将 Cosmbos DB Sql Api 中 Container 的主键命名为不同于 "id"

pagination - Cosmos DB .NET SDK V3 Query With Paging 示例需要

python - 尝试连接到 CosmosDB 数据库时出现 pymongo.errors.ServerSelectionTimeoutError