通过降序tokenvalue的Cosmos DB顺序

时间:2018-04-17 06:54:01

标签: azure-cosmosdb

我创建了一个帮助方法,使用Cosmos DB C#Library以continuationtoken的降序检索行。但是,当我通过降序尝试下单时,它不返回任何行。查询无需orderby或orderbydescending即可运行。请让我知道我需要更正以正确顺序检索行。

  public static async Task<(IEnumerable<T> resultRows, string responseContinuation)> GetIAlltemsAsyncOrderByCount(Expression<Func<T,bool>> predicate, Expression<Func<T,object>> orderby,bool descending, int MaxItemCountPerQuery,string continuationToken="")
    {
        var uri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);

        var feedOptions = new FeedOptions
        {
            MaxItemCount = MaxItemCountPerQuery
        };

        string responseContinuation = "";

        if (!string.IsNullOrEmpty(continuationToken))
        {
            feedOptions.RequestContinuation = continuationToken;
            responseContinuation = continuationToken;
        }

        var resultRows = new List<T>();

            using (var query = descending ? client.CreateDocumentQuery<T>(uri, feedOptions).Where(predicate).OrderByDescending(orderby).AsDocumentQuery() : client.CreateDocumentQuery<T>(uri, feedOptions).Where(predicate).OrderBy(orderby
                ).AsDocumentQuery())
            {


                if (query.HasMoreResults)
                {
                    var result = await query.ExecuteNextAsync<T>();

                    responseContinuation = result.ResponseContinuation;

                    resultRows.AddRange(result);


                    /* process result */

                }
            }

            return (resultRows, responseContinuation);

    }

1 个答案:

答案 0 :(得分:1)

根据你的描述,我在我这边检查了这个问题,发现我可能会遇到类似的问题。然后我使用Fiddler来捕获网络跟踪,如下所示:

enter image description here

indexing-policies所述:

  

索引类型:哈希(相等查询),范围(相等,范围或 ORDER BY查询)或空间(空间查询)。

     

Range支持高效的相等查询,范围查询(使用&gt;,&lt;,&gt; =,&lt; =,!=)和ORDER BY查询。 ORDER默认情况下,查询还需要最大索引精度(-1)。数据类型可以是String或Number。

我登录门户网站选择我的Cosmos DB帐户并修改Scale&amp ;;下的索引政策。我的收藏设置如下:

enter image description here

根据以上更改,我可以按字符串数据类型进行排序。

相关问题