2012-06-22 27 views
0

我有用戶之間的消息數據庫。文件看起來像:Couchbase/CouchDB按部分鍵和另一部分排序

"_id": "msg_0bec56c1-cbfb-47a5-8882-4a61fec332cd", 
"_rev": "1-00000eda4d07c93d0000009100000112", 
"$flags": 0, 
"$expiration": 0, 
"Date": 1340280439303, 
"OwnerId": 35, 
"RcptId": 37, 
"SndrId": 35, 
"Text": "msg5", 
"Unread": false, 
"id": "0bec56c1-cbfb-47a5-8882-4a61fec332cd", 
"type": "msg" 

對於每一個消息,它存儲2個文件不同OWNERID。我需要獲得一個指定人員與10個唯一「筆友」最後一條消息日期排序的最新消息

我的查詢參數:

降序=真& endkey = [35,35] & startkey = [35,35, 「{}」] &限制= 10 &跳過= 0

我的地圖功能:

功能(DOC){
(doc.OwnerId,doc.SndrId,doc.Date,doc.RcptId],doc);如果(doc.type ==「msg」){
        emit([doc.OwnerId,doc.RcptId,doc.Date,doc.SndrId],doc);
   }
}

結果我得到所需的帳戶信息列表。關鍵字中的最後一個值(第4個)是我們應該將值分組的值。

由於日期不同,導致羣組級別4不能正常工作。

+0

我創建了reduce函數,可以生成我需要的結果。但我不能將** limit = 10 **應用於它。 'function(keys,values,rereduce)var usrs = {}; var res = []; var i = 0; (key){if([usrs [key [0] [3]]){ usrs [key [0] [3]] = true; res。 keys.forEach推(值[I]); } i = i + 1; }); return res; //返回值; }' – m03geek

回答

0

這是我的新地圖功能,正是我想要的但只有當我有一臺服務器

var uniqPairs = []; 
function(doc){ 
    if (doc.type == "msg"){ 
    if (doc.OwnerId == doc.SndrId){ 
     if (uniqPairs.indexOf(doc.OwnerId + "_" + doc.RcptId) == -1){ 
     uniqPairs.push(doc.SndrId + "_" + doc.RcptId); 
     uniqPairs.push(doc.RcptId + "_" + doc.SndrId); 
     emit([doc.OwnerId, doc.Date], {"owner": doc.OwnerId, "from":doc.SndrId, "to":doc.RcptId}); 
     } 
    } 
    if (doc.OwnerId == doc.RcptId){ 
     if (uniqPairs.indexOf(doc.OwnerId + "_" + doc.SndrId) == -1){ 
     //uniqPairs.push(doc.SndrId + "_" + doc.RcptId); 
     uniqPairs.push(doc.RcptId + "_" + doc.SndrId); 
     emit([doc.OwnerId, doc.Date], {"owner": doc.OwnerId, "from":doc.SndrId, "to":doc.RcptId}); 
     } 
    } 
    } 
} 

所以對於集羣我犧牲了 「按日期排序」,並得到了地圖等功能/減少:

地圖:

function (doc) { 
    if (doc.type == "msg"){ 
    if (doc.OwnerId == doc.SndrId){ 
     emit([doc.OwnerId, doc.RcptId, doc.Date], {"Date":doc.Date, "OwnerId":doc.OwnerId, "RcptId":doc.RcptId, "SndrId":doc.SndrId, "Text":doc.Text, "Unread":doc.Unread, "id": doc.id, "type":doc.type}); 
    } else { 
     emit([doc.OwnerId, doc.SndrId, doc.Date], {"Date":doc.Date, "OwnerId":doc.OwnerId, "RcptId":doc.RcptId, "SndrId":doc.SndrId, "Text":doc.Text, "Unread":doc.Unread, "id": doc.id, "type":doc.type}); 
    } 
    } 
} 

減少:

var tmp = []; 
var t = []; 
function findRecent(arr){ 
    var max = 0; 
    var maxId = 0; 
    for (var i in arr){ 
    if (arr[i].Date > max){ 
     max = arr[i].Date; 
     maxId = i; 
    } 
    } 
    return arr[maxId]; 
} 
function(k,v,r){ 
    if (!r){ 
    tmp.push(v); 
    } 
    else{ 
    tmp.push(v); 
    } 
    if (r){ 
    for (var i1 in tmp[0]) 
    { 
     t.push(tmp[0][i1]); 
    } 
    } else { 
    return findRecent(v); 
    } 
    return findRecent(t); 
} 

如果有人知道更好的解決方案(即如何按日期排除結果) - 歡迎您回答。