如何在cosmos DB中计算不同的价值 [英] how to count distinct value in cosmos DB

查看:66
本文介绍了如何在cosmos DB中计算不同的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created some documents in Cosmos DB like this:

[
  {
    "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"
    }
  }
]

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

Could you help me how to count value of score in all data? My expectation is the result like this:

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

推荐答案

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

As @Sajeetharan said, group by is not supported by azure cosmos db so far. However , I suggest you using Stored Procedure to implement your requirement.

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

Base on your sample documents, I provide you with a sample stored procedure. Please refer to it.

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.');
}

输出:

Output :

希望它对您有帮助.

这篇关于如何在cosmos DB中计算不同的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆