CouchDb视图求和函数未给出预期结果

时间:2019-07-02 20:59:08

标签: json dictionary sum couchdb

我想计算我的文档中每个城市的建筑物数。这些文件与城市肯塔基州一起,我总共有6个城市。当我使用doc._id时,它返回ID。我只能显示城市名称。我似乎无法获得每个城市的建筑物总数。 如何显示每个城市的建筑总数?

CouchDB中的文档:

{
  "_id": "729489bb6702c473bb72254d13003f85",
  "_rev": "1-50da566becf84a0a686853290c2ad8a5",
  "Projects": [
    {
      "city": "Kentucky",
      "amount of people living": "853312",
      "overview": [
        {
          "building": "Center building",
          "amount of people": "150000",
          "teams": [
            {
              "party": "xd",
              "vote": "20000"
            },
            {
              "party": "zh",
              "vote": "20000"
            },
            {
              "party": "gh",
              "vote": "5000"
            },
            {
              "party": "sd",
              "vote": "20000"
            }
          ]
        },
        {
          "building": "Left building",
          "amount of people": "120000",
          "teams": [
            {
              "party": "gh",
              "vote": "20000"
            },
            {
              "party": "sw",
              "vote": "15000"
            },
            {
              "party": "gj",
              "vote": "5000"
            }
          ]
        },
        {
          "building": "Right building",
          "amount of people": "200000",
          "teams": [
            {
              "party": "sd",
              "vote": "20000"
            },
            {
              "party": "gs",
              "vote": "10000"
            },
            {
              "party": "er",
              "vote": "4000"
            }
          ]
        }
      ]
    }
  ]
}

我想要什么:

Key     | Value
---------------
Kentucky| 3      

我尝试使用但没有用的东西:

function (doc) {
var i;
  for( i=0; i < doc.Projects.length; i++){

  emit(doc.Projects[i].city,[doc.Projects[i].overview[i].building], 1);

  }
}

0 个答案:

没有答案