簡體   English   中英

CouchDB視圖(映射/縮小)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM