Azure cosmosdb 从Cosmos SQL获取分组(不同的?)值数组

Azure cosmosdb 从Cosmos SQL获取分组(不同的?)值数组,azure-cosmosdb,azure-cosmosdb-sqlapi,Azure Cosmosdb,Azure Cosmosdb Sqlapi,给定一个类似于以下内容的Cosmos文档集合,我想使用COSMOSSQL生成一个“类别”的分组(不同?!?)列表。在这方面的任何帮助都将不胜感激 [ { "id": "f0136e76-8e66-6a5a-3790-b577001d6420", "itemId": "analyze-and-visualize-your-data-with-azure-cosmos-db-notebooks", "title": "Built-in Jupyter notebooks

给定一个类似于以下内容的Cosmos文档集合,我想使用COSMOSSQL生成一个“类别”的分组(不同?!?)列表。在这方面的任何帮助都将不胜感激

[
  {
    "id": "f0136e76-8e66-6a5a-3790-b577001d6420",
    "itemId": "analyze-and-visualize-your-data-with-azure-cosmos-db-notebooks",
    "title": "Built-in Jupyter notebooks in Azure Cosmos DB are now available",
    "categories": [
      "Developer",
      "Database",
      "Data Science"
    ]
  },
  {
    "id": "f0136e76-8e66-6a5a-3790-b577001d6420",
    "itemId": "analyze-and-visualize-your-data-with-azure-cosmos-db-notebooks",
    "title": "Built-in Jupyter notebooks in Azure Cosmos DB are now available",
    "categories": [
      "Developer",
      "Database",
      "Data Science"
    ]
  },
  {
    "id": "d98c1dd4-008f-04b2-e980-0998ecf8427e",
    "itemId": "improving-azure-virtual-machines-resiliency-with-project-tardigrade",
    "title": "Improving Azure Virtual Machines resiliency with Project Tardigrade",
    "categories": [
      "Virtual Machines",
      "Supportability",
      "Monitoring"
    ]
  }
]
Azure CosmosDB目前不支持。您也可以使用存储过程来实现您的需求

根据上面给出的示例文档,下面是一个示例存储过程

function groupBy() {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var isValid = collection.queryDocuments(
        collectionLink,
        'SELECT * FROM stackoverflow s',
        {EnableCrossPartitionQuery: true},
    function (err, feed, options) {
        if (err) throw err;
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            console.log(JSON.stringify(response));
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();
            var items = {};
             for(var i=0;i<feed.length;i++){
                 var categories = feed[i].categories;
                 for(var j=0;j<categories.length;j++){
                        items[categories[j]] = categories[j]
                 }
                 var distinctArray = [];
                 for(var distinctObj in items){
                    distinctArray.push(items[distinctObj]);
                }
             }
            response.setBody(distinctArray);
        }
    });

    if (!isValid) throw new Error('Kindly check your query, which not accepted by the server.');
}
函数groupBy(){
var collection=getContext().getCollection();
var collectionLink=collection.getSelfLink();
var isValid=collection.queryDocuments(
收集链接,
“从堆栈溢出中选择*”,
{EnableCrossPartitionQuery:true},
功能(错误、馈送、选项){
如果(错误)抛出错误;
如果(!feed | |!feed.length){
var response=getContext().getResponse();
log(JSON.stringify(response));
响应。setBody(“未找到文件”);
}
否则{
var response=getContext().getResponse();
变量项={};
到目前为止,Azure CosmosDB不支持for(var i=0;i。您也可以使用存储过程来实现您的需求

根据上面给出的示例文档,下面是一个示例存储过程

function groupBy() {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var isValid = collection.queryDocuments(
        collectionLink,
        'SELECT * FROM stackoverflow s',
        {EnableCrossPartitionQuery: true},
    function (err, feed, options) {
        if (err) throw err;
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            console.log(JSON.stringify(response));
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();
            var items = {};
             for(var i=0;i<feed.length;i++){
                 var categories = feed[i].categories;
                 for(var j=0;j<categories.length;j++){
                        items[categories[j]] = categories[j]
                 }
                 var distinctArray = [];
                 for(var distinctObj in items){
                    distinctArray.push(items[distinctObj]);
                }
             }
            response.setBody(distinctArray);
        }
    });

    if (!isValid) throw new Error('Kindly check your query, which not accepted by the server.');
}
函数groupBy(){
var collection=getContext().getCollection();
var collectionLink=collection.getSelfLink();
var isValid=collection.queryDocuments(
收集链接,
“从堆栈溢出中选择*”,
{EnableCrossPartitionQuery:true},
功能(错误、馈送、选项){
如果(错误)抛出错误;
如果(!feed | |!feed.length){
var response=getContext().getResponse();
log(JSON.stringify(response));
响应。setBody(“未找到文件”);
}
否则{
var response=getContext().getResponse();
变量项={};

对于(var i=0;我成功了,谢谢。不幸的是,它只对单个分区键值有效;我想在我所有的分区上运行查询,这在进一步的研究中似乎是不可能的。Cosmos确实很酷,但我认为我不能将它用于我的场景;它需要组来工作。在任何情况下,感谢您及时和及时地完成有帮助的回答。我一定会接受你的答案,即使这不是我所希望的。这很有效,所以谢谢。不幸的是,它只对单个分区键值有效;我想在我所有的分区上运行查询,这在进一步的研究中似乎是不可能的。Cosmos真的很酷,但我不认为我可以将其用于我的方案;需要小组合作。无论是哪种情况,谢谢你及时而有益的回复。我一定会接受你的回答,即使这不是我所希望的。