[英]CouchDB Views (map/reduce)
我在CouchDB 1.6.1中有与此类似的文档:
"_id": "test_test",
"_rev": "4-8eb22214f7f3dccd3c979f95ded0a7b0",
"test": {
"sample": {
"abc": {
"aaa": 877,
"bbb": 202,
"ccc": 0,
"ddd": 362,
"eee": 242,
"ggg": 81
},
"def": {
"aaa": 697,
"bbb": 233,
"ccc": 0,
"ddd": 178,
"eee": 90,
"ggg": 5
},
"ghi": {
"aaa": 987,
"bbb": 396,
"ccc": 0,
"ddd": 399,
"eee": 178,
"ggg": 108
},
"jkl": {
"aaa": 1165,
"bbb": 332,
"ccc": 0,
"ddd": 286,
"eee": 173,
"ggg": 100
},
我希望从中提取数据,例如:
1)
"test": {
"sample": {
"abc": {
"aaa": 877,
"def": {
"aaa": 697,
"ghi": {
"aaa": 987, etc.
要么:
2)
"test": {
"sample": {
"def": {
"aaa": 697,
"bbb": 233,
"ccc": 0,
"ddd": 178,
"eee": 90,
"ggg": 5
},
我也希望能够获得所有带有'test'
文档或所有带有'sample'
的文档或带有'def'
所有文档或带有'aaa'
所有文档的总计。
我有一个地图JavaScript,如下所示:
function(doc) {
if(doc.test.sample.abc.aaa){
emit(null, doc.test.sample.abc.aaa);
}
}
正确地给出一个单一值(877)
我也尝试过
function(doc) {
for (var i in doc.test.sample.abc){
emit(doc.test.sample.abc[i], null);
}
}
希望得到:
"aaa": 877,
"bbb": 202,
"ccc": 0,
"ddd": 362,
"eee": 242,
"ggg": 81
有时候,使用map / reduce会成功,而有时候却没有任何效果。
遍历字段似乎很困难。
任何帮助将不胜感激。
编辑 :
CouchDB The Definitive Guide的“更改通知,过滤器”部分中的示例代码
{
"_id": "_design/app",
"_rev": "1-b20db05077a51944afd11dcb3a6f18f1",
"filters": {
"important": "function(doc, req) { if(doc.priority == 'high') { return true; }
else { return false; }}"
}
}
如果我将此代码粘贴到CouchDB中,它将被拒绝。 它还使JSONLint测试失败。 我尝试过以相同的结果将其缩小。 我在这里没看到什么? 下面的“答案2”中引用的代码通过JSONLint测试,CouchDB接受它。 但是,一旦开始进入特定功能,就会出错。 请问有没有办法解决这个问题。 再次非常感谢。
编辑2 :
我快到了吗??
Couch接受此代码:
{
"_id": "_design/lists",
"_rev": "3-628dc2f53051971994043c2e0bfc44ea",
"language": "javascript",
"views": {
"list_setup": {
"map": "function(doc) {if(doc.compartment.number){ emit(null, doc.compartment.number);}}"
}
},
"lists": {
"first_list": "function(head, req) {var result = []; while (row = getRow()) { if (row !== null) { row.key + row.value else{ send(JSON.stringify({status_code: 404}));}}send(JSON.stringify(result));}}"
}
}
但是,如果我卷曲,我就会
{"error":"compilation_error","reason":"Expression does not eval to a function. (function(head, req) {var result = []; while (row = getRow()) { if (row !== null) { row.key + row.value else{ send(JSON.stringify({status_code: 404}));}}send(JSON.stringify(result));}})"}
我不确定该函数的row.key + row.value
部分如何处理。
抱歉,这花了很长时间。
要完成您的第一个选择,您可以结合使用地图和列表功能。
使用地图功能,您可以为所有文档创建索引
function(doc){
emit(null, doc._id);
}
如果您想更具体地查询视图,则必须发出此特定值。
下一部分是列表功能。 如果使用“ include_docs = true”查询视图,则会返回整个文档。 在此基础上,您可以使用列表功能来格式化“前置”结果。 map函数的每个结果都位于包含对象的数组内(每个对象都等效于一个文档)。
这是我的列表功能模式:
function(head, req) {
var result = [];
while (row = getRow()) {
if (row !== null) {
// make your formatting here
else{
send(JSON.stringify({
status_code: 404
}));
}
}
send(JSON.stringify(result));
}
最终查询如下所示
curl -X GET -g'http:// [主机]:[端口] / [db] / _ design / [ddoc名称] / _ list / [列表功能名称] / [地图功能名称]?[query]&include_docs = true '
作为参考,这里是官方文档: http : //docs.couchdb.org/en/1.6.1/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name
这是设计文档中所有功能的完整参考。 我希望我不要错过任何括号;)
{
"_id": "_design/[ddoc name]",
"views": {
"[view name]": {
"map": "[map function]"
}
},
"language": "javascript",
"lists": {
"[list name]": "[list function]"
},
"shows": {
"[show name]": "[show function]"
},
"updates": {
"[update name]": "[update function]"
},
"filters": {
"[filter name]": "[filter function]"
}
}
最小化JavaScript代码也很重要。
很抱歉,我答复晚了。 我在度假。 您的语法不正确。
function(head, req) {
var result = [];
while (row = getRow()) {
if (row !== null) {
// This is not correct, you have to assigne the result to a
// variable.
// You also have to assigne something to the array result.
// result.append();
// So if row.key and row.value are both numbers and you want
// the result you have to write
// temp = row.key + row.value;
// result.append(temp);
//row.key + row.value
}else {
send(JSON.stringify({
status_code: 404
}));
}
}
send(JSON.stringify(result));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.