問題描述
我有兩個文檔,一個具有樹結構,另一個與第一個文檔相關.我試圖通過 fk 和 pk 加入這兩個文檔.我無法得到實際結果,它顯示所有空值.
I have two documents one with tree structure and the other one relation to the first doc. Im trying to join these two doc`s by fk and pk. I couldnt get the actual results and it displays all null values.
第一個文檔
{
"name": "one",
"root": {
"level1" : {
"level2" : {
"level3" : {
"itemone": "Randomkey1",
"itemtwo": "Randomkey2
}
}
}
},
"type": "firstdoc"
}
第二個文檔
{
"name" : "two",
"mapBy" : "Randomkey1",
"type" : "senconddoc
}
我編寫了一個 map 函數,它列出了給定級別 1 或 2 或 3 的所有鍵.現在我想使用密鑰加入第一個文檔和第二個文檔.我嘗試了兩種方法(第一種:我得到所有(Root,Randomkey),(docName,Randomkey1)但它沒有做任何加入.我正在尋找類似的結果(根,文檔名)
I`ve written a map function, which lists all the keys given a level 1 or 2 or 3 . Now I want o join this first doc and second doc using the key. Ive tried two ways (first: Im getting all (Root, Randomkey), (docName, Randomkey1) but it doesnt do any join. Im looking for a result like (Root, docName)
有人可以幫忙解決這個問題
Could someone assist in fixing this
地圖
function(doc) {
if (doc.type === 'firstdoc' || doc.type === 'seconddoc' ) {
var rootObj = doc.Root;
for (var level1 in rootObj) {
var level2Obj = doc.Root[level1];
for (var level2 in level2Obj) {
var keys = new Array();
var level3Obj = level2Obj[level2];
for (var i in level3Obj) {
var itemObj = level3Obj[i];
for (var i in itemObj) {
keys.push(itemObj[i]);
emit(doc.name, [itemObj[i], 0]);
var firstDocName = doc.name;
//This is gives null values
if (doc.Type === 'senconddoc' && doc.mapBy === itemObj[i]) {
emit(firstDocName , doc);
}
}
}
}
}
}
//This just lists keys to me
if (doc.type === 'senconddoc') {
emit([doc.mapBy, 1] , doc);
}
}
推薦答案
要模擬連接,你必須輸出一個帶有 _id
的文檔,_id
需要指向文檔的實際 _id
.然后你可以利用 include_docs=true
來拉取相關文檔.這里的多對多示例:http://danielwertheim.se/couchdb-多對多關系/
To simulate joins you have to output a doc with an _id
in it, the value of the _id
needs to point to an actual _id
of a document. Then you can make use of include_docs=true
to pull in the related documents. Example with many-to-many here: http://danielwertheim.se/couchdb-many-to-many-relations/
如果這不適用,您可以通過首先返回自定義鍵來進行兩步手動連接.然后對所有文檔視圖進行第二次查詢,指定多個鍵.
If this is not applicable, you can make a two step manual join by first returning custom keys. Then make a second query against the all documents view, with multiple keys specified.
這篇關于Couchdb 使用鍵連接兩個文檔的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!