我有一个查询,该查询用于计算特定类型的新旧文档的数量。
鉴于我试图在一个查询中获取一种以上类型的计数这一事实,很有可能应该重写查询,并且新版本可以在使用C#时按预期工作。
如果我使用“ New SQL Query”在Cosmos DB Data Explorer中运行查询,则该查询有效,但是当使用2.2.2
和2.8.1
(升级到最新版本)版本运行相同的查询时Microsoft.Azure.DocumentDB.Core
nuget包中的DocumentClient中没有。
提供以下文件:
[{
"PartitionKey": "partition",
"SomeKey": "foo",
"Type": "DocumentType",
"Value": "1",
"StaleAfterThis": 101
},
{
"PartitionKey": "partition",
"SomeKey": "bar",
"Type": "DocumentType",
"Value": "2",
"StaleAfterThis": 90
},
{
"PartitionKey": "partition",
"SomeKey": "bar",
"Type": "DocumentType",
"Value": "3",
"StaleAfterThis": 500
},
{
"PartitionKey": "partition",
"SomeKey": "foo",
"Type": "DocumentType",
"Value": "4",
"StaleAfterThis": 500
}]
以及以下查询:
SELECT
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = 'foo' AND c.PartitionKey = 'partition' AND c.StaleAfterThis < 100)) as StaleFooCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = 'foo' AND c.PartitionKey = 'partition' AND c.StaleAfterThis >= 100)) as FreshFooCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = 'bar' AND c.PartitionKey = 'partition' AND c.StaleAfterThis < 100)) as StaleBarCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = 'bar' AND c.PartitionKey = 'partition' AND c.StaleAfterThis >= 100)) as FreshBarCount
FROM c
WHERE
c.Type = 'DocumentType' AND
c.PartitionKey = 'partition'
在Data Explorer中基于Web的查询编辑器中运行此查询时,结果符合预期。
[
{
"StaleFooCount": 0,
"FreshFooCount": 1,
"StaleBarCount": 1,
"FreshBarCount": 1
}
]
如果我使用C#尝试相同的查询,则DocumentClient的所有值均为0。返回动态结果和键入结果时都是这种情况。
var queryOptions = new FeedOptions { MaxItemCount = 1 };
var queryText =
@"SELECT
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = '@foo' AND c.PartitionKey = '@partitionKey' AND c.StaleAfterThis < @staleAfterThis)) as StaleFooCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = '@foo' AND c.PartitionKey = '@partitionKey' AND c.StaleAfterThis >= @staleAfterThis)) as FreshFooCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = '@bar' AND c.PartitionKey = '@partitionKey' AND c.StaleAfterThis < @staleAfterThis)) as StaleBarCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = '@bar' AND c.PartitionKey = '@partitionKey' AND c.StaleAfterThis >= @staleAfterThis)) as FreshBarCount
FROM c
WHERE
c.Type = 'DocumentType' AND
c.PartitionKey = '@partitionKey'";
var queryParameters = new SqlParameterCollection
{
new SqlParameter("@partitionKey", "partition"),
new SqlParameter("@foo", FooBarEnum.Foo.ToString()),
new SqlParameter("@bar", FooBarEnum.Bar.ToString()),
new SqlParameter("@staleAfterThis", 100)
};
var sqlQuerySpec = new SqlQuerySpec(queryText, queryParameters);
var query = _client.CreateDocumentQuery<TempResult>(_collectionLink, sqlQuerySpec, queryOptions).AsDocumentQuery();
var totalStaleDocuments = new TempResult();
while (query.HasMoreResults)
{
foreach (var result in await query.ExecuteNextAsync<TempResult>())
{
totalStaleDocuments.FreshFooCount += result.FreshFooCount;
totalStaleDocuments.StaleFooCount += result.StaleFooCount;
totalStaleDocuments.FreshBarCount += result.FreshBarCount;
totalStaleDocuments.StaleBarCount += result.StaleBarCount;
}
}
public class TempResult
{
public int StaleFooCount { get; set; }
public int FreshFooCount { get; set; }
public int StaleBarCount { get; set; }
public int FreshBarCount { get; set; }
}
[
{
"StaleFooCount": 0,
"FreshFooCount": 1,
"StaleBarCount": 1,
"FreshBarCount": 1
}
]
[
{
"StaleFooCount": 0,
"FreshFooCount": 0,
"StaleBarCount": 0,
"FreshBarCount": 0
}
]
答案 0 :(得分:0)
您的C#查询不相同。在Portal查询中,您正在执行c.SomeKey = 'foo'
,而在C#中,您正在执行c.Type = '@foo'
最后,从查询参数中删除'
,如下所示:
var queryText =
@"SELECT
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = @foo AND c.PartitionKey = @partitionKey AND c.StaleAfterThis < @staleAfterThis)) as StaleFooCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = @foo AND c.PartitionKey = @partitionKey AND c.StaleAfterThis >= @staleAfterThis)) as FreshFooCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = @bar AND c.PartitionKey = @partitionKey AND c.StaleAfterThis < @staleAfterThis)) as StaleBarCount,
SUM((SELECT VALUE Count(1) WHERE c.SomeKey = @bar AND c.PartitionKey = @partitionKey AND c.StaleAfterThis >= @staleAfterThis)) as FreshBarCount
FROM c
WHERE
c.Type = 'DocumentType' AND
c.PartitionKey = @partitionKey";