2016-12-07 37 views
2

關鍵,我合併兩個表在表2 表1的主鍵存在,但在某些情況下,有在表2中沒有關鍵的外交時觸發的錯誤是沒有鍵存在。 我想合併表與條件只有存在的關鍵;RethinkDB:​​合併兩個表,如果一個不包含其他

r.table('sport').filter({sport_id:sport_id}).merge(function(doc){ 
    return { 
     terminology: r.table('sport_terminology').get(doc("terminology_id")) 
    } 
}) 
    .run(conn, sport); 

現在,如果運動不包含術語作爲關鍵比它是發射錯誤。如果鑰匙不存在,請給我合併旁路解決方案

回答

2

從你的問題來看,你有什麼困難,因爲你沒有發佈你得到的異常。

我假設對於某些文檔,您會在對象中得到「無屬性」terminology_id「。如果是這樣的情況下,只需添加一個default()值,e.g:

r.table('sport').filter({sport_id:sport_id}).merge(function(doc) { 
    return { 
    terminology: r.table('sport_terminology') 
     .get(doc("terminology_id").default(null)) 
    } 
}) 

如果不是的話,請張貼錯誤消息。

+0

感謝兄弟的幫助。正常工作 –

+0

爲什麼不使用'getAll'? 'r.table( '運動')GETALL( 「sport_id」,{索引: 「sport_id」})。'。這將加快您的查詢(對不起您的搜索),當然其他合併部分保持不變。 – pregmatch

相關問題