CouchDB组级别和密钥范围 [英] CouchDB Group Level and Key Range
问题描述
任何人都可以向我解释以下原因为何不起作用:
Can anyone explain to me why the following doesn't work:
假定以下文档结构:
{
"_id": "520fb089a6cb538b1843cdf3cca39a15",
"_rev": "2-f96c27d19bf6cb10268d6d1c34799931",
"type": "nosql",
"location": "AZ",
"date": "2012/03/01 00:00:00",
"amount": 1500
}
和这样定义的Map函数:
And a Map function defined like so:
function(doc) {
var saleDate = new Date(doc.date);
emit([doc.location,saleDate.getFullYear(),saleDate.getMonth()+1],doc.amount);
}
并使用内置的 _sum $ c
And using the built in _sum
function for the reducer.
执行此命令(使用group = true)时,您将得到如下结果:
When you execute this (with group=true) you get results like this:
{"rows":[
{"key":["AZ",2012,2],"value":224},
{"key":["AZ",2012,3],"value":1500},
{"key":["WA",2011,12],"value":1965},
{"key":["WA",2012,1],"value":358}
]}
现在,如果将查询更改为以下内容:
Now if you change the query to something like this:
http://127.0.0.1:5984/default/_design/nosql/_view/nosql_test?group_level=2
您将获得以下结果:
{"rows":[
{"key":["AZ",2012],"value":1724},
{"key":["WA",2011],"value":1965},
{"key":["WA",2012],"value":358}
]}
因此请记住,如果我想找出所有销售对于 WA,2011年我无法执行这样的操作:
So with that in mind if I wanted to find out all sales in 2011 for "WA" could I not execute something like this:
http://127.0.0.1:5984/default/_design/nosql/_view/nosql_test?group_level=2&key=["WA",2011]
此示例摘自NoSQL磁带上的有用视频。
This example has been taken from the useful videos over at NoSQL tapes.
http://nosqltapes.com/video/understanding-mapreduce-with-mike-miller
推荐答案
您始终需要提供一系列键,因为过滤是对 map
的结果进行的,< 减少
的强度>不。
You always need to give a range of keys, because filtering is done on map
's results, not on reduce
.
例如,以下参数应该可以工作(如果网址正确, -编码):
For example, the following parameters should work (if properly url-encoded):
?group_level=2&startkey=["WA",2011]&endkey=["WA",2011,{}]
您可以阅读有关查看排序规则以了解其工作原理。
You can read about view collation to understand how it works.
这篇关于CouchDB组级别和密钥范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!