代码之家  ›  专栏  ›  技术社区  ›  Nok Dev

Cosmos DB按标记值降序排列

  •  0
  • Nok Dev  · 技术社区  · 7 年前

    我使用Cosmos DB C#库创建了一个助手方法,使用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 回复  |  直到 7 年前
        1
  •  1
  •   Community CDub    5 年前

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

    enter image description here

    indexing-policies 声明如下:

    索引类型:哈希(相等查询)、范围(相等、范围或 按查询排序 ),或空间(空间查询)。

    范围支持高效的相等查询、范围查询(使用>、<、>=、<、!=)和按顺序查询。 默认情况下,按顺序查询还需要最大索引精度(-1) 。数据类型可以是字符串或数字。

    我登录门户选择我的Cosmos DB帐户,并在Scale&下修改索引策略;我的收藏设置如下:

    enter image description here

    基于上述更改,我可以按字符串数据类型排序。