【问题标题】:how to count distinct value in cosmos DB如何计算 cosmos DB 中的不同值
【发布时间】:2018-06-01 06:18:51
【问题描述】:

我在 Cosmos DB 中创建了一些文档,如下所示:

[
  {
    "class": "class01",
    "student": {
      "lastReport": [
        {
          "Name": "st01",
          "score": "C"
        },
        {
          "Name": "st02",
          "score": "B"
        }
      ],
      "lastTime": "2018-05-10"
    }
  },
  {
    "class": "class02",
    "student": {
      "lastReport": [
        {
          "Name": "st03",
          "score": "C"
        },
        {
          "Name": "st01",
          "score": "A"
        }
      ],
      "lastTime": "2018-05-10"
    }
  },
  {
    "class": "class01",
    "student": {
      "lastReport": [
        {
          "Name": "st01",
          "score": "C"
        },
        {
          "Name": "st02",
          "score": "A"
        },
        {
          "Name": "st03",
          "score": "B"
        }
      ],
      "lastTime": "2018-05-10"
    }
  }
]

您能帮我计算所有数据中的得分值吗?我的期望是这样的结果:

[
  {
    "score": "C",
    "Num" : 3
  },
{
    "score": "B",
    "Num" : 2
  },
{
    "score": "A",
    "Num" : 2
  }
  ]

【问题讨论】:

标签: azure azure-cosmosdb


【解决方案1】:

正如@Sajeetharan 所说,到目前为止,azure cosmos db 不支持 group by。但是,我建议您使用Stored Procedure 来实现您的要求。

根据您的示例文档,我为您提供了一个示例存储过程。请参考。

function sample() {
    var collection = getContext().getCollection();
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT a.score FROM  c join a in c.student.lastReport',
        function (err, feed, options) {
            if (err) throw err;
            if (!feed || !feed.length) getContext().getResponse().setBody('no docs found');
            else {
                var map = {};
                var returnResult = [];
                for(var i = 0;i<feed.length;i++){
                    var s = feed[i].score;
                    if(map.hasOwnProperty(s)){
                        map[s] ++;   
                    }else {
                        map[s] = 1;   
                    }         
                }

                for(var key in map){
                    console.log(key)
                    var obj = {
                        "score":key,
                        "num" : map[key]
                    };
                    returnResult.push(obj);
                }
                getContext().getResponse().setBody(returnResult);
            }
        });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

输出:

希望对你有帮助。

【讨论】:

    【解决方案2】:

    Cosmos DB 本身不支持 Group by,因此没有现成的方式来执行此查询。

    然而,documentdb-lumenize 允许您使用存储过程来执行此操作。我自己没有使用过,但可能值得研究。

    链接是:

    https://github.com/lmaccherone/documentdb-lumenize

    【讨论】:

      【解决方案3】:

      现在可以在 cosmosDB 中进行分组 本文档包含所有详细信息 azure docs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        相关资源
        最近更新 更多