查询cosmos db中的嵌套数组

man*_*ano 4 azure azure-cosmosdb

具有如下所述的文件列表。

文件1:

 {
    id:1,
    PostList:[
     {
         postname:"aaa",
         lastdatetime:2017-07-13T17:10:25+05:30,
         sname:"sas"
     },
     {
         postname:"aaa1",
         lastdatetime:2017-07-14T17:10:25+05:30,
         sname:"sasadd"
     },
     {
         postname:"aaa2",
         lastdatetime:2017-07-10T17:10:25+05:30,
         sname:"weq"
     }
     ]
}
Run Code Online (Sandbox Code Playgroud)

文件2:

 {
        id:2,
        PostList:[
         {
             postname:"aaa",
             lastdatetime:2017-07-13T17:10:25+05:30,
             sname:"sas"
         },
         {
             postname:"aaa1",
             lastdatetime:2017-07-14T17:10:25+05:30,
             sname:"sasadd"
         },
         {
             postname:"aaa2",
             lastdatetime:2017-07-10T17:10:25+05:30,
             sname:"weq"
         }
         ]
}
Run Code Online (Sandbox Code Playgroud)

我需要一个帖子名称列表,该列表等于“aaa”且 orderby lastdatetime。

我能够得到查询

select f.lastdatetime,f.postname
from c 
join f in c.PostList 
where f.postname='aaa' 
Run Code Online (Sandbox Code Playgroud)

但我需要使用 orderby lastdatetime 获取列表。

当我尝试以下查询时,出现错误

不支持过度相关集合的排序

select f.lastdatetime,f.postname
from c 
join f in c.PostList 
where f.postname='aaa' ORDER BY f.lastdatetime ASC
Run Code Online (Sandbox Code Playgroud)

有人有想法可以通过吗?

Jay*_*ong 6

正如 @Rafat Sarosh 在评论中所说:Order-by over correlated collections is not supported 它将在未来启用。

不过,我建议您使用一种解决方法来跟踪您的解决方案:使用Azure Cosmos DB UDF

您可以将查询结果作为参数传递给 UDF 进行排序处理。

查询SQL:

select f.lastdatetime,f.postname
 from c 
join f in c.PostList 
where f.postname='aaa' 
Run Code Online (Sandbox Code Playgroud)

UDF示例代码:

function userDefinedFunction(arr){
    var i = arr.length, j;
    var tempExchangVal;
    while (i > 0) {
        for (j = 0; j < i - 1; j++) {
            if (arr[j].lastdatetime < arr[j + 1].lastdatetime) {
                tempExchangVal = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tempExchangVal;
            }
        }
        i--;
    }
    return arr;
}
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助。