PouchDB分组和总结

时间:2018-04-08 11:28:08

标签: javascript mapreduce nosql pouchdb

我正在使用 pouchdb ,并且文档结构如下:

{
 "_id": "1",
 "title": "Title",
 "categories": {
   "category1": {
     "salesx": [
       {
         "date": "2016-09-15 00:00:00",
         "qty": "2"
       },
       {
         "date": "2016-09-16 00:00:00",
         "qty": "3"
       }
     ],
     "salesy": [
       [
         {
           "date": "2016-09-15 00:00:00",
           "qty": "1"
         }
       ]
     ]
   }
 },
 "_rev": "rev"
}

我现在想从pouchdb获得这样的结果:

{
"_id": "1",
"title": "Title",
"categories": {
  "category1": {
    "salesx": 2 + 3 = 5,
    "salesy": [
      [
        {
          "date": "2016-09-15 00:00:00",
          "qty": "1"
        }
      ]
    ]
  }
},
"_rev": "rev"
}

因此,应将 salesx 分组,并汇总数量。 salesy 不应更改。 我还想在日期字段中添加 where子句

我尝试从一个选择正确日期的地图功能开始。但是我以后不知道要把数量分组的东西。

function myMapFunction(doc) {
   doc.categories.forEach(category => {
       category.salesx.forEach(salex => {
           if (salex.date >= mydate) {
               emit(?)
           }
       })
   })
}

我无法在pouchdb docs找到一个好例子。它们仅提供将多个文档组合在一起的示例。

有谁知道如何对文档进行分组?

1 个答案:

答案 0 :(得分:0)

我修改了以下文档,将qty字段从字符串值转换为 number 值,以便能够进行数学运算:

{
  "_id": "sale0000",
  "_rev": "2-5cd6fff48f9fd099481d3b523ff1191f",
  "title": "Title",
  "categories": {
    "category1": {
      "salesx": [
        {
          "date": "2016-09-15 00:00:00",
          "qty": 2
        },
        {
          "date": "2016-09-16 00:00:00",
          "qty": 3
        }
      ],
      "salesy": [
        [
          {
            "date": "2012-09-15 00:00:00",
            "qty": 1
          }
        ]
      ]
    }
  }
}

正如你在评论中提到的那样:

  

所有文件看起来都与我已展示的文件完全一样。除了   salesx中的条目数和类别数(例如   category1category2 ...)已修改

根据您的上述陈述,我在我的示例数据库中创建了一些文档,这些文档在salesxcategoiry1category2等不同数量的条目中有不同数量的条目,但每个{ {1}}只有一个条目。

这是我的另一份示例文件:

salesy

我不确定我是否明白你的意图,但我开发了以下视图功能:

{
  "_id": "sale0002",
  "_rev": "3-12bf5c872fc124c1f0a5a4a108cb4f7a",
  "title": "Title",
  "categories": {
    "category1": {
      "salesx": [
        {
          "date": "2016-09-15 00:00:00",
          "qty": 2
        },
        {
          "date": "2016-09-16 00:00:00",
          "qty": 3
        },
        {
          "date": "2017-10-16 00:00:00",
          "qty": 9
        }
      ],
      "salesy": [
        [
          {
            "date": "2014-09-15 00:00:00",
            "qty": 1
          }
        ]
      ]
    },
    "category2": {
      "salesx": [
        {
          "date": "2018-01-15 00:00:00",
          "qty": 2
        },
        {
          "date": "2012-08-16 00:00:00",
          "qty": 3
        },
        {
          "date": "2015-10-16 00:00:00",
          "qty": 7
        }
      ],
      "salesy": [
        [
          {
            "date": "2011-03-15 00:00:00",
            "qty": 1
          }
        ]
      ]
    }
  }
}

在我的function (doc) { if(doc.categories){ for(var categ in doc.categories){ var salesx_qty_sum=0; for(var i=0, len=doc.categories[categ].salesx.length; i<len; i++){ salesx_qty_sum += doc.categories[categ].salesx[i].qty } emit( doc.categories[categ].salesy[0][0]['date'], //emit the date of salesy as the key //my understanding is that you need to index/sort //according to date of salesy [ salesx_qty_sum, //emit the sum of qty for salesx as value doc.categories[categ].salesy[0][0]['qty'] //emit qty for salesy (unchanged) as value ] ); } } } 数据库中,我创建了四个类似于sample的条目数不同的文档,以及不同数量的类别。每个salesx内只有一个条目:

salesy

上面的视图地图(名为 $ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_all_docs' {"total_rows":53,"offset":0,"rows":[ ... ... {"id":"_design/by_date","key":"_design/by_date","value":{"rev":"20-04cb881624aa819dac13f69c688f2124"}}, ... ... {"id":"sale0000","key":"sale0000","value":{"rev":"2-5cd6fff48f9fd099481d3b523ff1191f"}}, {"id":"sale0001","key":"sale0001","value":{"rev":"2-e8ee019fb0ad076b82d3f4772f736559"}}, {"id":"sale0002","key":"sale0002","value":{"rev":"3-12bf5c872fc124c1f0a5a4a108cb4f7a"}}, {"id":"sale0003","key":"sale0003","value":{"rev":"2-6d40368ddd16b9d02b908ea95d061c7e"}} ]} )正在索引我的文档,如下所示,其中by_datekey的日期。根据您的描述,我假设每个类别中每个salesy应该只有一个条目。此外,下面的salesy是一个数组,其中第一个元素是value的{​​{1}}的总和,qty的第二个元素不变salesx:< / p>

qty

您可以使用salesy$ curl -k -X GET 'https://admin:admin@192.168.1.106:6984/sample/_design/by_date/_view/by_date' {"total_rows":6,"offset":0,"rows":[ {"id":"sale0002","key":"2011-03-15 00:00:00","value":[12,1]}, {"id":"sale0000","key":"2012-09-15 00:00:00","value":[5,1]}, {"id":"sale0001","key":"2013-05-20 00:00:00","value":[12,1]}, {"id":"sale0002","key":"2014-09-15 00:00:00","value":[14,1]}, {"id":"sale0003","key":"2015-04-15 00:00:00","value":[5,1]}, {"id":"sale0003","key":"2018-04-05 00:00:00","value":[5,1]} ]} 查询by_date视图,以获取两个日期之间的结果,如下所示:

startkey